帮助
失败的原因:
Origin checking failed - https://praktikum6.jhoncena.repl.co 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设置自定义此页面。
发布于 2021-12-12 18:19:56
检查是否使用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
在Django 4.0中更改: 在旧版本中不执行源标头检查。
发布于 2022-02-20 12:12:04
2022年3月最新情况:
如果您的django版本是"4.x.x"
python -m django --version
// 4.x.x
然后,如果错误如下所示:
原产地检查失败- https://example.com不匹配任何可信的来源。
将此代码添加到"settings.py"中
CSRF_TRUSTED_ORIGINS = ['https://example.com']
在您的例子中,您得到了以下错误:
原产地检查失败- https://praktikum6.jhoncena.repl.co不匹配任何可信的来源。
因此,您需要将此代码添加到 "settings.py"中。
CSRF_TRUSTED_ORIGINS = ['https://praktikum6.jhoncena.repl.co']
发布于 2022-03-15 13:13:31
起源和宿主是相同的域。
如果像我一样,当源和主机是相同的域时,就会收到此错误。
这可能是因为:
settings.py
,例如SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
和/或proxy_set_header X-Forwarded-Proto https;
。在这种情况下:
https://www.example.com
。request.is_secure()
正在返回False
。_origin_verified()
返回False
,因为django.middleware.csrf第285行 ( https://www.example.com
与http://www.example.com
的比较): def _origin_verified(self, request):
request_origin = request.META["HTTP_ORIGIN"]
try:
good_host = request.get_host()
except DisallowedHost:
pass
else:
good_origin = "%s://%s" % (
"https" if request.is_secure() else "http",
good_host,
)
if request_origin == good_origin:
return True
确保您在更改此设置之前阅读了 https://docs.djangoproject.com/en/4.0/ref/settings/#secure-proxy-ssl-header 中的警告!
https://stackoverflow.com/questions/70285834
复制相似问题