帮助
给出的失败原因:
Origin checking failed - https://praktikum6.jhoncena.repl.co does not match any trusted origins.
一般来说,当存在真正的Cross Site Request Forgery,或者没有正确使用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 设置自定义此页面。
原文由 Erico Fahri 发布,翻译遵循 CC BY-SA 4.0 许可协议
检查您是否使用 Django 4.0。我使用的是 3.2,并在升级到 4.0 时遇到了这个问题。
如果您使用的是 4.0,这是我的解决方案。将此行添加到您的
settings.py
。当我使用 3.2 时这不是必需的,现在我不能在没有它的情况下发布包含 CSRF 的表单。CSRF_TRUSTED_ORIGINS = ['https://*.mydomain.com','https://*.127.0.0.1']
查看此行是否需要任何更改,例如,如果您需要将
https
http
。根本原因是在 4.0 中添加了原始标头检查。
https://docs.djangoproject.com/en/4.0/ref/settings/#csrf-trusted-origins