Django 1.8静态文件不工作 [英] Django 1.8 static files doesnt work
本文介绍了Django 1.8静态文件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用1.8版本的Django和我的CSS,JS或图像不工作,我真的不知道为什么,我的项目文件夹是像
项目
Jetint
静态
CSS
我的设置:
DEBUG = True
ALLOWED_HOSTS = []
#应用程序定义
INSTALLED_APPS =(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django .contrib.staticfiles',
'clients',
'front',
)
MIDDLEWARE_CLASSES =(
'django.contrib.sessions.middleware .SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django .middleware.security.SecurityMiddleware',
)
ROOT_URLCONF ='jetint.urls'
TEMPLATES = [
{
'BACKEND ':'django.template.backends.django.DjangoTemplates',
'DIRS':['jetint / templates'],
'APP_DIRS':True,
'OPTIONS':{
'context_processors':[
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth ',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION ='jetint.wsgi.application'
#数据库
#https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'bd',
'USER':'bd',
'PASSWORD':'xxxx',
'HOST':'',
'PORT':'',
'ATOMIC_REQUESTS' :True
}
}
TEMPLATE_DIRS =(BASE_DIR,'jetint / templates')
#国际化
#https:// docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE ='en-us'
TIME_ZONE ='UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#静态文件(CSS,JavaScript,图片)
#https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL ='/ static /'
STATIC_ROOT =(
os。 path.join(BASE_DIR,'/ static /'),
)
STATICFILES_DIRS =(STATIC_ROOT,)
和我的base.html
<!DOCTYPE html&
< html lang =en>
< head>
< title> - {%block title-section%} {%endblock%}< / title>
< meta charset =utf-8>
< meta name =format-detectioncontent =telephone = no/>
{#< link rel =iconhref ={{STATIC_URL}} images / favicon.ico> #}
{#< link rel =shortcut iconhref ={{STATIC_URL}} images / favicon.ico/> #}
{%load staticfiles%}
< link rel =stylesheethref ={{STATIC_URL}} css / camera.css>
< link rel =stylesheethref ={{STATIC_URL}} css / stuck.css>
< link rel =stylesheethref ={{STATIC_URL}} css / style.css>
但无效,css,js文件或图像。请帮助?
编辑:
现在我的html看起来像这样:
{%load staticfiles%}
<!DOCTYPE html>
< html lang =en>
< head>
< title> Jet Internacional - {%block title-section%} {%endblock%}< / title>
< meta charset =utf-8>
< meta name =format-detectioncontent =telephone = no/>
{#< link rel =iconhref ={{STATIC_URL}} images / favicon.ico> #}
{#< link rel =shortcut iconhref ={{STATIC_URL}} images / favicon.ico/> #}
< link rel =stylesheethref ={%static'css / camera.css'%>
< link rel =stylesheethref ={%static'css / stuck.css'%>
< link rel =stylesheethref ={{%static'css / style.css'%>
< script src ={{STATIC_URL}} js / jquery.js>< / script>
< script src ={{STATIC_URL}} js / jquery-migrate-1.1.1.js>< / script>
< script src ={{STATIC_URL}} js / script.js>< / script>
< script src ={{STATIC_URL}} js / superfish.js>< / script>
< script src ={{STATIC_URL}} js / jquery.equalheights.js>< / script>
< script src ={{STATIC_URL}} js / jquery.mobilemenu.js>< / script>
< script src ={{STATIC_URL}} js / jquery.easing.1.3.js>< / script>
< script src ={{STATIC_URL}} js / tmStickUp.js>< / script>
< script src ={{STATIC_URL}} js / jquery.ui.totop.js>< / script>
< script src ={{STATIC_URL}} js / camera.js>< / script>
<! - [if(gt IE 9)|!(IE)]><! -
< script src ={{STATIC_URL}} js / jquery.mobile.customized.min.js>< / script>
<! - <![endif] - >
< script src ={{STATIC_URL}} js / jquery-ui-1.10.3.custom.js>< / script>
< script>
$(document).ready(function(){
jQuery('#camera_wrap')。camera({
loader:false,
pagination:false,
minHeight:'200',
thumbnailails:false,
height:'25 .625%',
caption:false,
navigation:true,
fx:'mosaic'
});
$()。UItoTop({easingType:'easeOutQuart'});
$('#stuck_container')。tmStickUp({});
} ;
$(function(){
jQuery(#accordion).accordion({
collapsible:true,
heightStyle:content,
});
});
< / script>
<! - [if lt IE 8]>
{%comment%}< div style ='clear:both; text-align:center; position:relative;'>
< a href =http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode>
< img src =http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpgborder =0height =42width =820alt =您正在使用过时的浏览器。要获得更快,更安全的浏览体验,请立即免费升级。 />
< / a>
< / div> {%endcomment%}
<![endif] - >
<! - [if lt IE 9]>
< script src =js / html5shiv.js>< / script>
< link rel =stylesheetmedia =screenhref =css / ie.css>
<![endif] - >
< / head>
< body class =page1id =top>
<! - ============================
header
== ============================= - >
< header>
<! - ============================
卡住菜单
= ============================ - >
< section id =stuck_container>
< div class =container>
< div class =row>
< div class =grid_12>
< h1>
< a href =/>
< img src ={%static'images / logo-rif.png'%}width =200px; alt =Logo alt>
< / a>
< / h1>
< div class =header_socials>< a href =#class =fa fa-facebook>< / a>< a href =#class = -twitter>< / a>< a
href =CLASS =fa fa-instagram>< / a>
{#< a href ={%url'registo_usuario'%}>注册< / a> #}
< / div>
和我的设置...
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))
#快速开始开发设置 - 不适合生产
#查看https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
#安全警告:保持生产秘密中使用的密钥!
SECRET_KEY ='m9y8koy5%cm4 @ _ = sk_hjlbl $ z%mgj * 8dztu13 @)^ zr(j * g @ q0m'
#安全警告:在生产中!
DEBUG = True
ALLOWED_HOSTS = []
#应用程序定义
INSTALLED_APPS = b $ b'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'clients',
'front',
)
MIDDLEWARE_CLASSES =(
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
' django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF ='jetint.urls'
TEMPLATES = [
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
'DIRS':['jetint / templates'],
' APP_DIRS':True,
'OPTIONS':{
'context_processors':[
'django.template.context_processors.debug',
'django.template.context_processors.request' ,
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
} ,
]
WSGI_APPLICATION ='jetint.wsgi.application'
#数据库
#https://docs.djangoproject。 com / en / 1.8 / ref / settings /#databases
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'bd',
'USER':'bd',
'PASSWORD':'...',
'HOST':'',
'PORT':'',
'ATOMIC_REQUESTS':True
}
}
TEMPLATE_DIRS =(BASE_DIR,'jetint / templates')
#国际化
#https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE ='en-us'
TIME_ZONE ='UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#静态文件(CSS,JavaScript,Images)
#https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL =' jetint / static /'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static')
STATICFILES_DIRS =(
os.path.join (os.path.dirname(BASE_DIR),'static'),
)
解决方案将
{%load staticfiles%}
放在html文档的最顶端。
也请尝试:
STATIC_URL ='/ static /'
STATIC_ROOT = os.path.join .path.dirname(BASE_DIR),'static')
STATICFILES_DIRS =(
os.path.join(os.path.dirname,BASE_DIR),'static'),
)
- - - 编辑:
此外,要在html模板中调用静态文件,请遵循以下格式:
< link href ={%static'css / camera.css'%} =stylesheetmedia =screen>
Im using the 1.8 version of Django and my CSS, JS or images are not working, i really dont know why, my project folder is like
Project Jetint Static CSS
My settings:
DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'clients', 'front', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', ) ROOT_URLCONF = 'jetint.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['jetint/templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'jetint.wsgi.application' # Database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'bd', 'USER': 'bd', 'PASSWORD': 'xxxx', 'HOST': '', 'PORT': '', 'ATOMIC_REQUESTS': True } } TEMPLATE_DIRS = (BASE_DIR, 'jetint/templates') # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = ( os.path.join(BASE_DIR, '/static/'), ) STATICFILES_DIRS = (STATIC_ROOT,)
and my base.html
<!DOCTYPE html> <html lang="en"> <head> <title> - {% block title-section%}{% endblock %}</title> <meta charset="utf-8"> <meta name = "format-detection" content = "telephone=no" /> {# <link rel="icon" href="{{ STATIC_URL }}images/favicon.ico"> #} {# <link rel="shortcut icon" href="{{ STATIC_URL }}images/favicon.ico" /> #} {% load staticfiles %} <link rel="stylesheet" href="{{ STATIC_URL }}css/camera.css"> <link rel="stylesheet" href="{{ STATIC_URL }}css/stuck.css"> <link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">
but nothing works, css, js files, or images. please help?
EDIT:
now my html looks like this:
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <title> Jet Internacional - {% block title-section%}{% endblock %}</title> <meta charset="utf-8"> <meta name = "format-detection" content = "telephone=no" /> {# <link rel="icon" href="{{ STATIC_URL }}images/favicon.ico"> #} {# <link rel="shortcut icon" href="{{ STATIC_URL }}images/favicon.ico" /> #} <link rel="stylesheet" href="{% static 'css/camera.css' %"> <link rel="stylesheet" href="{% static 'css/stuck.css' %"> <link rel="stylesheet" href="{{ % static 'css/style.css'%"> <script src="{{ STATIC_URL }}js/jquery.js"></script> <script src="{{ STATIC_URL }}js/jquery-migrate-1.1.1.js"></script> <script src="{{ STATIC_URL }}js/script.js"></script> <script src="{{ STATIC_URL }}js/superfish.js"></script> <script src="{{ STATIC_URL }}js/jquery.equalheights.js"></script> <script src="{{ STATIC_URL }}js/jquery.mobilemenu.js"></script> <script src="{{ STATIC_URL }}js/jquery.easing.1.3.js"></script> <script src="{{ STATIC_URL }}js/tmStickUp.js"></script> <script src="{{ STATIC_URL }}js/jquery.ui.totop.js"></script> <script src="{{ STATIC_URL }}js/camera.js"></script> <!--[if (gt IE 9)|!(IE)]><!--> <script src="{{ STATIC_URL }}js/jquery.mobile.customized.min.js"></script> <!--<![endif]--> <script src="{{ STATIC_URL }}js/jquery-ui-1.10.3.custom.js"></script> <script> $(document).ready(function(){ jQuery('#camera_wrap').camera({ loader: false, pagination: false , minHeight: '200', thumbnails: false, height: '25.625%', caption: false, navigation: true, fx: 'mosaic' }); $().UItoTop({ easingType: 'easeOutQuart' }); $('#stuck_container').tmStickUp({}); }); $(function() { jQuery( "#accordion" ).accordion({ collapsible: true, heightStyle: "content", }); }); </script> <!--[if lt IE 8]> {% comment %}<div style=' clear: both; text-align:center; position: relative;'> <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode"> <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." /> </a> </div>{% endcomment %} <![endif]--> <!--[if lt IE 9]> <script src="js/html5shiv.js"></script> <link rel="stylesheet" media="screen" href="css/ie.css"> <![endif]--> </head> <body class="page1" id="top"> <!--============================== header =================================--> <header> <!--============================== Stuck menu =================================--> <section id="stuck_container"> <div class="container"> <div class="row"> <div class="grid_12"> <h1> <a href="/"> <img src="{% static 'images/logo-rif.png' %}" width="200px;" alt="Logo alt"> </a> </h1> <div class="header_socials"><a href="#" class="fa fa-facebook"></a><a href="#" class="fa fa-twitter"></a><a href="" CLASS="fa fa-instagram"></a> {# <a href="{% url 'registo_usuario' %}"> Registro </a> #} </div>
and my settings...
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'm9y8koy5%cm4@_=sk_hjlbl$z%mgj*8dztu13@)^zr(j*g@q0m' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'clients', 'front', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', ) ROOT_URLCONF = 'jetint.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['jetint/templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'jetint.wsgi.application' # Database # https://docs.djangoproject.com/en/1.8/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'bd', 'USER': 'bd', 'PASSWORD': '...', 'HOST': '', 'PORT': '', 'ATOMIC_REQUESTS': True } } TEMPLATE_DIRS = (BASE_DIR, 'jetint/templates') # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = 'jetint/static/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static') STATICFILES_DIRS = ( os.path.join(os.path.dirname(BASE_DIR), 'static'), )
解决方案Put
{% load staticfiles %}
at the very top of the html document.Also try:
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static') STATICFILES_DIRS = ( os.path.join(os.path.dirname(BASE_DIR), 'static'), )
- - - Edit:
Additionally, to call static files in html templates, follow this format:
<link href="{% static 'css/camera.css' %}" rel="stylesheet" media="screen">
这篇关于Django 1.8静态文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文