我在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條目的順序如何。這條線的設計使我不必在部署時進行更改。