[英]Django: Can't render STATIC_URL from settings in template
http://docs.djangoproject.com/en/dev/howto/static-files/ http://docs.djangoproject.com/en/dev/howto/static-files/
This suggests that I can use STATIC_URL
in my template to get the value from settings.py.这表明我可以在模板中使用
STATIC_URL
从 settings.py 获取值。
Template looks like this:模板如下所示:
<link href="{{STATIC_URL}}stylesheets/tabs.css" rel="stylesheet" type="text/css" media="screen" />
Settings.py looks like this: Settings.py 看起来像这样:
STATIC_ROOT = ''
STATIC_URL = '/static/'
When I go to the page I just get <link href="stylesheets/tabs.css"
ie no STATIC_URL.当我转到该页面时,我只得到
<link href="stylesheets/tabs.css"
即没有 STATIC_URL。
What am I missing?我错过了什么?
You have to use context_instance=RequestContext(request)
in your render_to_response
, for example:您必须在
render_to_response
使用context_instance=RequestContext(request)
,例如:
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
Or use the new shortcut render或者使用新的快捷方式渲染
As Dave pointed out, you should check if django.core.context_processors.static
is in your TEMPLATE_CONTEXT_PROCESSORS
variable in settings.py.正如 Dave 指出的,您应该检查
django.core.context_processors.static
是否在 settings.py 中的TEMPLATE_CONTEXT_PROCESSORS
变量中。 As the docs said, it`s there by default.正如文档所说,默认情况下它就在那里。
It is not recommended to directly use the STATIC_URL
variable.不建议直接使用
STATIC_URL
变量。 See the accepted answer in this question请参阅此问题中已接受的答案
Instead of代替
{{STATIC_URL}}stylesheets/tabs.css
use用
{% load staticfiles %}
{% static 'stylesheets/tabs.css' %}
I have the same problem, solved like this:我有同样的问题,解决方法如下:
in settings.py add:在 settings.py 中添加:
django.template.context_processors.static
here:这里:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_DIRS,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.