获取每个页面上包含的 templatetag 的 RequestContext
我有一个生成表单的模板标签,因此需要 {% csrf_token %}
来保证安全,而这又需要一个 RequestContext
对象而不是标准的 Context对象。
现在,相关的特定模板标签/表单包含在每个页面的标题中。目前,每个页面都不使用 RequestContext
对象来render_to_response
。
有什么方法可以解决这个问题,而不必更改整个站点中的每个视图来传递 RequestContext
?
提前致谢。
I have a templatetag which generates a form, this therefore requires the {% csrf_token %}
for security, which in turn requires a RequestContext
object instead of the standard Context
object.
Now, the particular templatetag/form in question is included in the header of every page. Currently every page doesn't render_to_response
with a RequestContext
object.
Is there any way in which I can get around this issue without having to changing every view within the entire site to pass through the RequestContext
?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管可能有一些解决方法,但最佳实践肯定是在各处添加 RequestContext。这对于其他中间件也有优势。
但还有一个遗留方法,将在 Django 1.4 中删除,不需要您更改所有视图。我建议不要这样做,除非你没有太多选择。
http://docs.djangoproject.com/en/dev/参考/contrib/csrf/#legacy-method
All though there may be some workarounds, the best practice is definitely to add the RequestContext everywhere. This has advantages for other middlewares as well.
But there is also a legacy method, that will be removed in Django 1.4 that does not require you to change all your views. I would recommend against it unless you don't have much option.
http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#legacy-method