1

当我访问我的静态文件 (http://127.0.0.1:8000/site_media/static/css/site_base.css) 的 url 时,我收到 404 错误消息。

来自日志的 404

[27/Oct/2012 14:14:54] "GET /site_media/static/js/libs/modernizr-2.5.2.min.js HTTP/1.1" 404 1716
[27/Oct/2012 14:34:20] "GET /site_media/static/css/site_base.css HTTP/1.1" 404 1677

目录结构

mysite
    |-- manage.py
    |-- mysite
         |-- settings
             |-- base.py
             |-- dev.py
             |-- prod.py
    |-- site_media
       |-- static
           |-- css
               |-- site_base.css

site_base.html

<link rel="stylesheet" href="{{ STATIC_URL }}css/site_base.css" />

设置

TEMPLATE_CONTEXT_PROCESSORS = [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.static",
]

STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
]

STATIC_URL = "/site_media/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")

更新:

网址.py

from django.conf import settings
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^grappelli/', include('grappelli.urls')),
    url(r'^profile/', include('profiles.urls')),
)

urlpatterns += staticfiles_urlpatterns()
4

2 回答 2

1

简短的回答

Django不在STATIC_URL 上提供 STATIC_ROOT,除非你也告诉它

长答案

这应该是一个常见问题解答,这是一篇关于正确使用 django.contrib.staticfiles的文章(免责声明:由我撰写,感谢反馈)。它就像文档,但试图更短更实用。

于 2012-10-27T19:49:03.847 回答
1

使用<link rel="stylesheet" href="{{ STATIC_URL }}css/site_base.css" />而不是

<link href="{%static 'css/style.css'%}" rel="stylesheet"> 并确保您的值 STATIC_URL = '/static/'在 settings.py

于 2019-05-30T02:06:53.610 回答