Django 1.2 is consistently giving me this CSRF verification error when I perform a POST form. I "think" I've done all the things asked in the Django 1.2 docs, namely,
当我执行POST表单时,Django 1.2一直给我这个CSRF验证错误。我“想”我已经完成了Django 1.2文档中提到的所有事情,即
-
Ensure MIDDLEWARE_CLASSES is included with 'django.middleware.csrf.CsrfViewMiddleware'
确保MIDDLEWARE_CLASSES包含在'django.middleware.csrf.CsrfViewMiddleware'中
-
Ensure the {% csrf_token %}
确保{%csrf_token%}
<form action="/words/new/" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Enter" /> </form>
-
Use RequestContext in my response
在我的回复中使用RequestContext
def create(request): if request.method == 'POST': form = DefinitionForm(request.POST) if form.is_valid(): form.save() c = {} return render_to_response('dict/thanks.html',c, context_instance=RequestContext(request)) else: form = DefinitionForm() return render_to_response('dict/create_definition.html', { 'form' : form, })
Note that the GET action works in this function. So I think I'm using render_to_response right.
请注意,GET操作适用于此功能。所以我认为我正在使用render_to_response。
I've even tried to throw in the @csrf_protect decorator and even that didn't seem to work. I'm out of ideas and I'm about to choke myself with my laptop.
我甚至试图投入@csrf_protect装饰器,即使这似乎没有用。我没有想法,我打算用笔记本电脑掐自己。
Any thing you guys can think of?
你们能想到的任何事情?
Thanks!
谢谢!
1 个解决方案
#1
7
You're not following #3. The RequestContext
must be used with the rendering of the template that shows the form. It's not necessary for the thanks page.
你没有关注#3。 RequestContext必须与显示表单的模板的呈现一起使用。谢谢页面没有必要。
return render_to_response('dict/create_definition.html', {
'form' : form,
}, context_instance=RequestContext(request))
And as a side note, you should use the PRG pattern instead of rendering the thanks page directly.
作为旁注,您应该使用PRG模式而不是直接渲染感谢页面。
#1
7
You're not following #3. The RequestContext
must be used with the rendering of the template that shows the form. It's not necessary for the thanks page.
你没有关注#3。 RequestContext必须与显示表单的模板的呈现一起使用。谢谢页面没有必要。
return render_to_response('dict/create_definition.html', {
'form' : form,
}, context_instance=RequestContext(request))
And as a side note, you should use the PRG pattern instead of rendering the thanks page directly.
作为旁注,您应该使用PRG模式而不是直接渲染感谢页面。