模板中的 django 请求
我已经启用了 Django 请求处理器,
TEMPLATE_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
但我仍然不需要请求模板中可用的变量。 我必须手动传递它。 使用 Django 1.0.2。 在网络上的任何地方,似乎都只与启用的请求处理器有关。
我还使用 RequestContext
作为:
return render_to_response(
'profile.html',
{
'persons':Person.objects.all(),
'person':Person.objects.get(id=id),
'request':request,
},
context_instance=RequestContext(request)
)
不运气。
哦,该死的新名称是TEMPLATE_CONTEXT_PROCESSORS
I've enabled the Django request processor
TEMPLATE_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Still I don't have to request variable available in templates. I've to manually pass it. Using Django 1.0.2. Everywhere on web it seems it's only about enabled request processor.
Also I am using RequestContext
as:
return render_to_response(
'profile.html',
{
'persons':Person.objects.all(),
'person':Person.objects.get(id=id),
'request':request,
},
context_instance=RequestContext(request)
)
No luck.
ohh darn the new name for that is TEMPLATE_CONTEXT_PROCESSORS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
设置.py:
settings.py:
TEMPLATE_CONTEXT_PROCESSORS
代替
模板处理器
TEMPLATE_CONTEXT_PROCESSORS
instead of
TEMPLATE_PROCESSORS
请注意,从 Django 1.8 开始,这已更改为“TEMPLATES”设置,并且在默认配置中,不包括请求处理器。
只需重新添加请求处理器即可解决问题:
有关详细信息,请参阅 Django升级文档。
Be advised that as of Django 1.8, this has changed to a "TEMPLATES" setting, and in the default configuration, the request processor is NOT included.
Just add the request processor back in to fix the issue:
For more info, see the Django Upgrading Docs.
您确定模板没有可用的
request
变量吗? 行时会发生什么当您删除与存在时不同的 。 如果您的模板以任何一种方式加载都相同,则问题出在您的模板上。
Are you sure you don't have the
request
variable available to the template? What happens when you remove the linethat's different from when that line is present. If your template loads the same either way, the problem is with your template.
中间件_类=(
...
'你的文件夹.你的文件.你的类',
...
你的类:
类 AddRequestToTemplate:
process_templaet_response(自身,请求,响应):
response.context_data['请求']=请求
MIDDLEWARE_CLASSES=(
...
'yourfolder.yourfile.yourclass',
...
yourclass:
class AddRequestToTemplate:
process_templaet_response(self, request, response):
response.context_data['request']=request