2011-12-26 95 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}}模板标记,而不是试图让它以这种方式提供网址。在部署时,您仍然不需要进行更改,这样您就可以移动某些东西,而不用担心要处理另一个上下文变量。