Django:模板渲染期间出错

时间:2017-03-18 09:36:23

标签: python django django-templates

我正在尝试构建反馈表单,但我收到此错误: 无法解析剩余部分:来自'%csrf_token%'的'%csrf_token%'

这是我的views.py:

def contact(request):
    if request.method=='POST':
        form=ContactForm(request.POST)
            if form.is_valid():
            topic=form.cleaned_data['topic']
            message=form.cleaned_data['message']
            sender=form.cleaned_data.get('sender')
            send_mail(
            'Feedback from your site,topic:%s'%topic,
            message,
            sender,
            ['jpahultiwari@gmail.com']
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form=ContactForm()
    context={'form':form}
    return render(request,'blog/contact.html',context)

这是我的模板contact.html:

<!DOCTYPE html>
<html>
<head>
    <title>Feedback Form</title>
</head>
<body>
<h1>Contact Us</h1>
<form  action="." method="post" >
{{% csrf_token %}}
<table>{{form.as_table}}</table>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:5)

错字:

将此{{% csrf_token %}}更改为此{% csrf_token %}

然而,由于Django的回溯非常详细,并且指出了导致错误的原因,因此您可以很容易地发现这些错误。

祝你好运!

相关问题