相关疑难解决方法(0)

Django - CSRF验证失败

尝试从教程中创建一个简单的表单时,我收到CSRF验证失败消息.我对CSRF验证实际上做了一些研究,据我所知,为了使用它你需要在你的html中使用其中一个csrf_token标签,但我没有

这是我的模板:

<form action="/testapp1/contact/" method="post">
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

相当简单,位于contact.html

这是我的urlconf:来自django.conf.urls.defaults import*

urlpatterns=patterns('testapp1.views',
    (r'^$', 'index'),
    (r'^contact/$','contact')
)
Run Code Online (Sandbox Code Playgroud)

应用名称为testapp1.当我输入我的网址(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 …
Run Code Online (Sandbox Code Playgroud)

python django

23
推荐指数
1
解决办法
4万
查看次数

标签 统计

django ×1

python ×1