我在WinVista上使用Django 1.3.1和Python 2.7。无论是在本地开发者服务器还是部署到我的主机时,我都遇到同样的问题。Django静态文件只加载网站的首页
我的网站上静止媒体的主要页面显示:
http://www.drugpolicyreformmovement.com
在二级页面的CSS,图像等不显示:
http://www.drugpolicyreformmovement.com/newsarchive2003
http://www.drugpolicyreformmovement.com/newsarchive2010
或
http://www.drugpolicyreformmovement.com/newsarchive2009
'manage runserver'的输出显示了这些辅助'newsarchive'页面上的静态媒体的404错误。不管怎样,'document_root'在辅助页面上与主页面不同,因此它会在这些辅助页面中查看'/ newsclippings2003/static',而不是只看'/ static',因为它应该和喜欢它为首页。
我不知道我的URLconf什么是与你有关的,所以我已经包括了整个文件的位置:
import os
from django.conf.urls.defaults import *
from django.views.generic import ListView, YearArchiveView
from newsclippings.models import Article
from drugpolicyreformmovement.views import ArticleYearArchiveView
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', ListView.as_view(
queryset = Article.objects.order_by("-date", "publication", "author", "headline"),
context_object_name='articles',
template_name='index.html')),
(r'^newsarchive(?P<year>\d+)/$', ArticleYearArchiveView.as_view()),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root' : os.path.join(os.path.dirname(__file__), 'static') }),
# url(r'^drugpolicyreformmovement/', include('drugpolicyreformmovement.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
同样,我觉得这是有问题的行:
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root' : os.path.join(os.path.dirname(__file__), 'static') }),
无论我放置URLconf条目的顺序如何。这条线的设计使我不必在部署时进行更改。