django静态文件未加载 [英] django static file not loading

查看:174
本文介绍了django静态文件未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我按照教程 https:// docs .djangoproject.com / en / 1.5 / intro / tutorial06 / ,每个东西也运行正常,但CSS,图像没有显示他们的效果。作为一个新的django,需要建议。感谢任何帮助。



我的css文件: -

  li a {
color:red;
}
body {
background:white url(images / background.gif)no-repeat right bottom;
}

url.py文件: -


$ b从django.conf导入设置$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b从投票导入视图
urlpatterns = patterns('',
url(r'^ $',views.IndexView.as_view(),name ='index'),
url r'^(?P< pk> \d +)/ $',views.DetailView.as_view(),name ='detail'),
url(r'^(?P< pk> \d + )/ results / $',views.ResultsView.as_view(),name ='results'),
url(r'^(?P< poll_id> \d +)/ vote / $',views.vote ,name ='vote'),
)+ static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)

Index.html文件 -

  {%load staticfiles%} 
< link rel = style =text / csshref ={%static'/polls/style.css'%}/>
{%if latest_poll_list%}
< ul>
{%在latest_poll_list%}中的投票
< li>< a href ={%url'polls:detail'poll.id%}> {{poll.question}}< ; / A>< /锂>
{%endfor%}
< / ul>
{%else%}
< p>没有轮询可用。< / p>
{%endif%}

Settings.py文件: -

  MEDIA_ROOT =''
MEDIA_URL =''
STATIC_ROOT =''
STATIC_URL ='/ static /'
STATICFILES_DIRS =(
'/ polls / static /'

STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
' django.contrib.staticfiles.finders.AppDirectoriesFinder',


TEMPLATE_LOADERS =(
'django.template.loaders.filesystem.Loader',
'django。 template.loaders.app_directories.Loader',


MIDDLEWARE_CLASSES =(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions。中间件SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware' ,


ROOT_URLCONF ='m ysite.urls'

WSGI_APPLICATION ='mysite.wsgi.application'

TEMPLATE_DIRS =(
'C:/ django poll project / mysite / templates',


INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions' ,
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'polls',

在运行服务器上,获取 -



[18 / Sep / 2014 17:40:51]GET / polls / HTTP / 1.1200 311
[18 / Sep / 2014 18:25:39] GET / polls / HTTP / 1.1200 311

解决方案

静态文件的URL为yourDomain / static /



当您想访问style.css时,您应该使用/static/style.css而不是/polls/style.css p>

编辑:



更改此部分您的settings.py

  STATICFILES_DIRS =(
'/ polls / static /'

  STATICFILES_DIRS =(
'C:/ django poll project / mysite / static'

更好的是:

  STATICFILES_DIRS =(
os.path.join(SITE_ROOT,'。 ','static'),

然后文件夹被称为静态并且位于manage.py所在的同一级别。当您将style.css放在此静态文件夹中时,可以使用/static/style.css调用它。


I have problem that i done every thing as described in tutorial https://docs.djangoproject.com/en/1.5/intro/tutorial06/ and every thing also running fine but css,images are not showing their effects. Being a new one on django, Suggestions required. thanks for any help.

My css file:-

li a {
    color: red;
}
body {
    background: white url("images/background.gif") no-repeat right bottom;
}

url.py file:-

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns,url
from polls import views
urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'), 
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Index.html file -

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static '/polls/style.css' %}"/>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
    <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

Settings.py file:-

MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/polls/static/'
    )
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'

TEMPLATE_DIRS = (
    'C:/django poll project/mysite/templates',
    )

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'polls',
)

On runserver, getting-

[18/Sep/2014 17:40:51] "GET /polls/ HTTP/1.1" 200 311 [18/Sep/2014 18:25:39] "GET /polls/ HTTP/1.1" 200 311

解决方案

The URL to the static-Files is "yourDomain/static/"

When you want to access to your "style.css" you should use "/static/style.css" instead of "/polls/style.css"

EDIT:

Change this part of your settings.py

STATICFILES_DIRS = (
    '/polls/static/'
    )

to

STATICFILES_DIRS = (
    'C:/django poll project/mysite/static'
    )

better would be:

STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, '..', 'static'),
)

Then the folder is called "static" and is on the same level where the "manage.py" is. When you put your style.css in this "static"-folder you can call it with "/static/style.css"

这篇关于django静态文件未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆