【发布时间】:2010-12-28 16:43:02
【问题描述】:
我在尝试根据教程制作简单表单时收到 CSRF 验证失败消息。我对 CSRF 验证实际上是什么做了一些研究,据我所知,为了使用它,您需要在 html 中使用这些 csrf_token 标记之一,但我没有那个
这是我的模板:
<form action="/testapp1/contact/" method="post">
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
相当简单,位于contact.html
这是我的 urlconf: 从 django.conf.urls.defaults 导入 *
urlpatterns=patterns('testapp1.views',
(r'^$', 'index'),
(r'^contact/$','contact')
)
应用名称为 testapp1。当我输入我的 url (http://localhost:8000/testapp1/contact) 时,我正确地转到了表单。然后当我提交表单时,我收到验证错误。
以下是我的观点,虽然我认为这无关紧要:
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
cc_myself = form.cleaned_data['cc_myself']
recipients = ['info@example.com']
if cc_myself:
recipients.append(sender)
print 'Sending Mail:'+subject+','+message+','+sender+','+recipients
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render_to_response('contact.html', {
'form': form,
})
【问题讨论】: