Django CSRF令牌不会显示 [英] Django CSRF token won't show

查看:154
本文介绍了Django CSRF令牌不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是模板中HTML的相关代码段:

Here's the relevant snippet of HTML in the template:

    <form action="/submit_text/" method="post">
    {% csrf_token %}
    {% include "backbone/form_errors.html" %}
    {{form.as_p}}
    <input type="submit" value="Submit" />
    </form>

这是我的 settings.py MIDDLEWARE_CLASSES 声明:

MIDDLEWARE_CLASSES = ( 
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

CSRF令牌根本不显示,导致

The CSRF token simply doesn't show, causing a


禁止(403)CSRF验证失败。请求已终止。

Forbidden (403) CSRF verification failed. Request aborted.


推荐答案

您需要传递 RequestContext 在您的 render_to_response 中,上下文处理器将实际运行。

You need to pass the RequestContext in your render_to_response for the context processors to actually be run.

 from django.template import RequestContext

 context = {}
 return render_to_response('my_template.html',
                           context,
                           context_instance=RequestContext(request))

新的 render 快捷键(django 1.3+)将为您完成:

the new render shortcut (django 1.3+) will do it for you:

 from django.shortcuts import render

 context = {}
 return render(request, 'my_template.html', context)

这篇关于Django CSRF令牌不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆