【问题标题】:django: render versus render_to_response and its relation to csrf_tokendjango:render 与 render_to_response 及其与 csrf_token 的关系
【发布时间】:2012-02-05 17:16:15
【问题描述】:

我通常在 django 中为我的视图函数使用 render_to_response。

但是,最近,我一直在尝试使用更多的渲染方法 (https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render),正如我从文档中了解的那样, 如果未提供,则自动包含“context_instance”。

这个可选的 context_instance 参数如何与 crsf_token 相关联?我是否必须使用类似

的东西显式发送模板变量,例如“csrf(request)”
template_vars.update(csrf(request))

我问这个是因为我得到一个莫名其妙的

"Forbidden (403) CSRF verification failed. Request Aborted". 

即使我在我的 settings.py 和我的模板、表单、{% csrf_token %} 模板标签调用中启用了 csrf 中间件。

【问题讨论】:

    标签: django django-csrf


    【解决方案1】:

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

    【讨论】:

    • 不。很遗憾。我已经包含在 TEMPLATE_CONTEXT_PROCESSORS
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2018-09-18
    相关资源
    最近更新 更多