Reason given for failure: CSRF token missing or incorrect.
来源:6-3 用户登录-2
慕田峪8152788
2019-02-26
在使用管理员账号登录之后,能够进入主页面,但是用户名仍然是bobby, 刷新之后就会出现下面的错误,并没有像视频中一样可以依然保持登录状态
禁止访问 (403)
CSRF验证失败. 相应中断.
报错信息:
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 when Django’s CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function passes a request to the template’s render method.
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 use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You’re seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
请问这是什么原因呢?
已经在form中添加了{% csrt_token %}
1回答
-
Ethreal
2019-02-27
你需要看看对应的模板文件是不是存在 input hidden 类似
这样的东西 在模板文件中 需要删除掉 ,然后在重启项目刷新下页面就可以了 ,至于用户名是bobby的问题 往后看 课程后面会在模板做一些判断逻辑处理 这个在后面都有讲到 {% csrt_token %} 标签要放在form闭合标签的上面,最好不要随便放 引起未知错误062019-03-03
相似问题
这样的东西 在模板文件中 需要删除掉 ,然后在重启项目刷新下页面就可以了 ,至于用户名是bobby的问题 往后看 课程后面会在模板做一些判断逻辑处理 这个在后面都有讲到 {% csrt_token %} 标签要放在form闭合标签的上面,最好不要随便放 引起未知错误