Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.
In general, this can occur when there is a genuine Cross Site Request Forgery, or whenDjango’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
- The view function uses
RequestContextfor the template, instead ofContext. - In the template, there is a
{% csrf_token %}template tag inside each POST form that targets an internal URL. - If you are not using
CsrfViewMiddleware, then you must usecsrf_protecton any views that use thecsrf_tokentemplate tag, as well as those that accept the POST data.
解决办法:在视图中:
from django.template import RequestContext
return render_to_response("books/contact.html",{"form":form},context_instance=RequestContext(request))
本文详细解释了Django中常见的CSRF验证失败错误,并提供了具体的解决方案。主要针对POST请求时出现的CSRF token缺失或不正确的问题,给出了配置RequestContext及使用csrf_protect等解决方法。
1661

被折叠的 条评论
为什么被折叠?



