django:渲染与 render_to_response 及其与 csrf_token 的关系
我通常在 django 中使用 render_to_response 作为我的视图函数。
然而,最近,我一直在尝试使用更多的渲染方法(https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render),据我从文档中了解到,它自动包括如果未提供,则为“context_instance”。
这个可选的 context_instance 参数如何与 crsf_token 相关?我是否必须显式发送诸如“csrf(请求)”之类的模板变量,使用类似
template_vars.update(csrf(request))
我询问此问题的内容,因为我得到了一个莫名其妙的信息
"Forbidden (403) CSRF verification failed. Request Aborted".
即使我在我的settings.py和我的模板中启用了csrf中间件,形式,一个 {% csrf_token %} templatetag 调用。
I have usually been using render_to_response for my view functions in django.
However, recently, I have been trying to use more of the render method (https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render) which as I understand here from the docs, automatically includes 'context_instance' if none is provided.
How does this optional context_instance argument work in relation to crsf_token? Do I have to explicitly send in a template variable such as "csrf(request)", using something like
template_vars.update(csrf(request))
I am asking about this because I am getting an inexplicable
"Forbidden (403) CSRF verification failed. Request Aborted".
Even though I have the csrf middleware enabled in my settings.py and in my template, form, a {% csrf_token %} templatetag call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否忘记将
django.core.context_processors.csrf
添加到TEMPLATE_CONTEXT_PROCESSORS
中?Did you forget to add
django.core.context_processors.csrf
toTEMPLATE_CONTEXT_PROCESSORS
?