2011-12-26 97 views
1

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

回答

2

你在第一頁的網址是

http://www.drugpolicyreformmovement.com/static/css/blueprint/print.css 
在頁內

http://www.drugpolicyreformmovement.com/newsarchive2003/static/css/blueprint/print.css 

只需添加/在URL或使用{{ STATIC_URL }}

例如

  • /static/css/blueprint/print.css

  • <img src="{{ STATIC_URL }}css/blueprint/print.css" />

只是在設置中設置STATIC_ROOT

在這裏看到:

1

當我查看源html,我看到相對您的靜態資產的路徑。您需要使用絕對路徑!

這是錯誤的:

<link rel="stylesheet" href="static/css/blueprint/screen.css" media="screen, projection"> 

使用此:

<link rel="stylesheet" href="/static/css/blueprint/screen.css" media="screen, projection"> 

最有可能是你的模板,它是不正確的,但你並沒有顯示你的模板文件。

1

您應該使用模板中的{{STATIC_URL}}模板標記,而不是試圖讓它以這種方式提供網址。在部署時,您仍然不需要進行更改,這樣您就可以移動某些東西,而不用擔心要處理另一個上下文變量。