缺少或错误的CSRF令牌

25 投票
10 回答
104315 浏览
提问于 2025-04-17 06:05

我刚开始学习Django,已经花了很长时间在解决这个问题上了。我的中间件类里确实有'django.middleware.csrf.CsrfViewMiddleware',而且我的表单里也有这个令牌。

这是我的代码,我哪里做错了呢?

from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from chartsey.authentication.forms import RegistrationForm
from django.template import RequestContext
from django.core.context_processors import csrf

def register(request):

    if request.method == 'POST':
        c = RequestContext(request.POST, {})
        form = RegistrationForm(c)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/")
    else:
        form = RegistrationForm()

    return render_to_response("register.html",  {'form': form,  }, )

这是我的模板:

{% block content %}

    <h1>Register</h1>
    <form action="" method="POST"> {% csrf_token %}
        {{ form.as_p }}
    <input type="submit" value="Submit">
    </form>

{% endblock %}

10 个回答

4

试着用render来代替render_to_response

from django.shortcuts import render

render(request, "foo.html", {})

Django - render()、render_to_response()和direct_to_template()之间有什么区别?

正如上面链接所说,这个功能是在Django 1.3中引入的,它会自动使用RequestContext

6

只需要把这个加到你的视图里

return render_to_response("register.html", {'form': form, }, context_instance = RequestContext(request))

这样就可以了!!

30

更新:这个回答来自2011年。现在的CSRF防护很简单。

现在你应该使用 render 这个快捷函数,像这样 return render(request, 'template.html'),它会自动使用 RequestContext,所以下面的建议已经过时8年了。

  1. 使用 render https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/
  2. 添加CSRF中间件 https://docs.djangoproject.com/en/2.2/ref/csrf/
  3. 使用 {% csrf_token %} 模板标签
  4. 确认你能看到CSRF令牌的值被生成,并且在你的表单请求中提交

原始回复

我猜测你在模板中有这个标签,但它没有渲染出任何内容(或者你是说你在实际的HTML中确认了CSRF令牌正在生成?)

可以使用 RequestContext 代替字典

render_to_response("foo.html", RequestContext(request, {}))

或者确保你的 CONTEXT_PROCESSORS 设置中有 django.core.context_processors.csrf

https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

或者手动将令牌添加到你的上下文中

撰写回答