【发布时间】:2012-06-10 13:27:10
【问题描述】:
我收到错误消息,“CSRF 令牌丢失或不正确”,但我相信我在模板中包含了正确的标签。以下是一直显示此错误的视图和模板:
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
return HttpResponseRedirect('/contact/thanks/')
else:
form = ContactForm()
return render_to_response('reserve/templates/contact_form.html',{'form': form})
模板:
<html>
<head>
<title>Contact us</title>
</head>
<body>
<h1>Contact us</h1>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
【问题讨论】:
-
如果您查看模板 HTML 源代码,您是否看到设置了 csrf 令牌字段?
-
还要确保您已将中间件添加到 settings.py 或正在使用 @csrf_protect 装饰器。
标签: python django templates csrf