django:渲染与 render_to_response 及其与 csrf_token 的关系

发布于 2024-12-25 15:26:46 字数 543 浏览 5 评论 0原文

我通常在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-01-01 15:26:46

您是否忘记将 django.core.context_processors.csrf 添加到 TEMPLATE_CONTEXT_PROCESSORS 中?

Did you forget to add django.core.context_processors.csrf to TEMPLATE_CONTEXT_PROCESSORS?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文