django错误 - Reason given for failure: CSRF cookie not set.
练习Django表单提交时遇到如下问题:
在网上各种查找,终于找到了解决方法。
1.在from 表单中添加 {% csrf_token %}
2.在视图中添加 from django.template import RequestContext 导入项,并且在return 返回中添加context_instance=RequestContext(request)
<form role="form" method="post" action="login"> {% csrf_token %} <input name="userName" type="text" class="text" placeholder="账号" required="" > <input name="password" type="password" placeholder="密码" required > <div class="submit"><input type="submit" value="登录"></div> </form>
from django.template import RequestContext def index(request): return render_to_response("login.html",context_instance=RequestContext(request)) def login(request): return render_to_response("index.html")
再测试就OK了。
django之所以引进CSRF是为了避免Cross Site Request Forgeries攻击