python - 仅加载站点首页的 Django 静态文件

标签 python django

我在 WinVista 上使用 Django 1.3.1 和 Python 2.7。无论是在本地开发服务器上还是部署到我的主机上,我都遇到了同样的问题。

在我网站的主页静态媒体显示:

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 条目的顺序无关紧要。此行旨在让我在部署时不必进行更改。

最佳答案

第一页的网址是

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

在内页

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

只需添加 /在网址中或使用 {{ STATIC_URL }}

例如

  • /static/css/blueprint/print.css

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

只需在设置中设置 STATIC_ROOT

看这里:

关于python - 仅加载站点首页的 Django 静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8632819/

相关文章:

python - 如何旋转数据框

python - Django 中的图像字段

python - VSCode 无法识别 venv(Pyt​​hon)

python - Heroku 部署 - PermissionError : [Errno 13] Permission denied: '/etc/passwd-'

python - 如果列表长度未知(Python),如何确定列表中的所有数字是否相同?

python - 正则表达式严格检查字符串中的数字

mysql - 添加外键后 Django 迁移出现 "unknown column"错误

python - 无法在heroku上恢复postgres转储

python - 无法使用 docker-compose up 运行服务器

django - 如何使表单标签和字段显示在 django-crispy-form 的同一行