Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins(禁止(403)CSRF验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配)
本文介绍了禁止(403)CSRF验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
帮助
失败原因:
Origin checking failed - https://praktikum6.jhoncena.repl.co does not match any trusted origins.
通常,如果存在真正的跨站点请求伪造,或者没有正确使用Django的CSRF机制,就会出现这种情况。对于发布表单,您需要确保:
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设置来自定义此页面。
推荐答案
检查您是否正在使用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中的更改:
在旧版本中不执行源头检查。
这篇关于禁止(403)CSRF验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:禁止(403)CSRF验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配


猜你喜欢
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01