我在 django 项目中使用超级管理员登录,想要更改用户的一些属性,但是当我单击“保存”按钮时,它显示错误,如下所示:
禁止 (403)
CSRF 未经过验证。请求已取消。
帮助
失败原因:
Origin checking failed - https://somedomain.com does not match any trusted origins.
一般来说,当存在真正的跨站请求伪造,或者没有正确使用 Django 的 CSRF 机制时,就会发生这种情况。对于 POST 表单,您需要确保:
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.
The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
您看到此页面的帮助部分是因为您的 Django 设置文件中有 DEBUG = True。将其更改为 False,则仅显示初始错误消息。
您可以使用 CSRF_FAILURE_VIEW 设置自定义此页面。
我是 stackoverflow 的新手,这是我的第一个问题,请帮助我!
您需要将此行添加到您的
settings.py
CSRF_TRUSTED_ORIGINS = ['https://example.com']
如果您想包含子域,您可以添加它。
CSRF_TRUSTED_ORIGINS = ['https://*.example.com']
这将允许包含所有子域。