禁止(403)CSRF验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配 [英] Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins

查看:47
本文介绍了禁止(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验证失败。请求已中止。失败原因:来源检查失败与任何受信任的来源都不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆