<html lang="en">
<head>
<title>Contact us</title> </head>
<body>
<h1>Contact us</h1>
<form action="." method="POST">
<table> {{ form.as_table }} </table>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
以上我的测试template,form是一个forms对象,访问对应的URL的时候可以正常显示预定的页面,但是当点击提交按钮的时候就会出现“CSRF token missing or incorrect.“报错,报错页面中也提供了处理办法,基本就是 requestContext 用 Context()代替,然后在template中的post形式form中加入{% csrf_token %}。
按照其中的说法作了,对应template如下:
<html lang="en">
<head>
<title>Contact us</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>
但是没有效果,后来发现还需要在setting.py中加入对应的“MIDDLEWARE_CLASSES”。
对应加入:
'django.middleware.csrf.CsrfResponseMiddleware',
然后运行,OK。