我有一个模板,base.html
,在其他几个模板中用于各种视图。每个模板都以相应的{% extends "base.html" %}
开头。在基本模板,我想正是如此指定静态样式表:STATIC_URL在基础Django模板中未定义
<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。
这使我朝着正确的方向前进。 (你引用的部分是[here](https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext))我没有调用'render_to_response' context_instance =的RequestContext(请求)'。 – UltraNurd
不错!很高兴为您提供帮助;) – danihp
如果您使用的是Django 1.3,则无需手动传入context_instance。渲染快捷方式将为您执行此操作:return render(request,'the-template.html',{'foo':'bar'}) – Brandon