【问题标题】:Django: Error during template renderingDjango:模板渲染期间出错
【发布时间】:2017-03-18 09:36:22
【问题描述】:

我正在尝试构建反馈表,但出现此错误: 无法解析余数:'% csrf_token %' from '% 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>

【问题讨论】:

  • 如果此答案帮助您将其标记为已接受。在 StackOverflow 中这样做是一个很好的做法。
  • 很抱歉延迟接受您的回答。谢谢...

标签: python django django-templates


【解决方案1】:

这里有错别字:

把这个{{% csrf_token %}}改成这个{% csrf_token %}

但是,由于 Django 的回溯非常详细,并且指向导致错误的原因,因此这些错误很容易被您发现。

最好的问候!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 2017-05-21
    • 2017-07-03
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2017-11-13
    • 2020-08-04
    相关资源
    最近更新 更多