【发布时间】:2011-12-02 19:29:42
【问题描述】:
我有一个模板base.html,它在其他几个模板中用于各种视图。这些模板中的每一个都以相应的{% extends "base.html" %} 开头。在基本模板中,我想指定一个静态样式表:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/base.css"/>
但是,当它渲染我的大部分模板时,STATIC_URL 的值是空的,所以属性只是href="/base.css",它不会加载。该变量是为我绑定到默认登录视图django.contrib.auth.views.login 的模板正确定义的,但对于我自己的自定义视图,它是未定义的。
我只是想通过runserver 让它在开发环境中工作,因此 CSS 文件位于应用程序的静态子目录中。
以下是我的settings.py 中的相关位,这些都是默认值:
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
在我的urls.py 中我也添加了:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#...
urlpatterns += staticfiles_urlpatterns()
任何想法我做错了什么?据我所知,这是您应该根据the 1.3 documentation 在开发中为特定于应用程序的静态文件提供服务时应该做的事情。
【问题讨论】: