CSRF verification failed. Request aborted.
展开阅读全文

当运行一个django项目后,出现了这样的一个错误,如图:

CSRF verification failed. Request aborted.

You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.

If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for “same-origin” requests.

这是因为网页上会进行安全校验。 

方法一:

  我们可以在settings.py中注释掉一行即可。这一行大概在46行左右。

'django.middleware.csrf.CsrfViewMiddleware'

方法二:

  在html页面的form标签下加上

{%csrf_token%}

方法三:

  views.py上导入

from django.views.decorators.csrf import csrf_exempt

  然后在自己写的函数上面加上

@csrf_exempt