我正在运行 Django 1.9.6
如果我尝试在模板中使用 {{ request.path }}
,则 var/tag 为空
这是我的设置.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, '_templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
],
'builtins': ['django.templatetags.i18n']
},
},
]
我读到,在 1.8 版本之后,您只需要包含 'django.template.context_processors.request'
,您就会在模板中运行 var
如果在我的 View 中添加,我就能解决该问题
return render_to_response('admin-users/events.html', {}, context_instance=RequestContext(request))
我做错了什么,因为我读了很多答案,都说我不必添加 RequestContext
?
最佳答案
请改用 render
方法,它会自动使用 RequestContext
来呈现模板。
return render(request, 'admin-users/events.html', {})
不再推荐使用 render_to_response
快捷方式。
关于python - {{ 请求 }} var 空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37596690/