当页面中form使用POST方式向后台提交时,报如下错误:

禁止访问 (403)

CSRF验证失败. 请求被中断.

Help

Reason given for failure:

​ CSRF token missing or incorrect.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

  • 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.

    You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.

    You can customize this page using the CSRF_FAILURE_VIEW setting.

在网上查找了很多资料,普遍采用的解决方法是:

  1. 检查settings.py中是否有下面的配置,如没有加上:

    MIDDLEWARE_CLASSES = (

    'django.middleware.common.CommonMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware', # 确认存在

    'django.contrib.auth.middleware.AuthenticationMiddleware',

    'django.contrib.messages.middleware.MessageMiddleware',

    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    )

  2. html中的form添加模板标签{% csrf_token %}

    <form action="/saveBlog/" method="post">

    {% csrf_token %}

    ...

    </form>

  3. 针对views.py进行修改

    from django.shortcuts import render_to_response

    from django.template import RequestContext



    def some_blog(request):

    # ...

    return render_to_response('my_template.html',

    my_data_dictionary,

    context_instance=RequestContext(request))

    以上各步都基本照做了。其中针对视图的修改,因我的写法与网上不一样,我的是:

    def edit_blog(request):

    template = get_template('editBlog.html')

    moods = Mood.objects.all()

    html = template.render(locals())

    ``

    return HttpResponse(html)

    因为官方文档上说renderrender_to_responseshortcut,所以没有照网站资料上说的去改。于是错误总是无法解决。

几番挣扎之后,终于在Django出错页面中自己给出的解决办法中有一条:The view function passes a request to the template's rendermethod.(即:视图函数中传递 request 给模板的render方法)。

按这个访求修改后,终于解决问题了!!

def edit_blog(request):

template = get_template('editBlog.html')

moods = Mood.objects.all()

html = template.render(locals(), request) # 把request传递给template.render()

``

return HttpResponse(html)

Django CSRF验证失败. 请求被中断.的更多相关文章

  1. Django项目报错: 禁止访问(403),CSRF验证失败,相应中断

    如果想要取消表单的CSRF防护,可以在模板上删除{% csrf_token %}, 并且在相应的视图函数中添加装饰器@csrf_exempt, 代码如下: from django.views.deco ...

  2. Django在form提交CSRF验证失败. 相应中断问题

    CSRF验证失败. 相应中断. 1).首先,我们可以先看一下出现问题的所在的原因. Your browser is accepting cookies. The view function passe ...

  3. Django中ajax发送post请求,报403错误CSRF验证失败解决办法

    今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败:先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了CSRF的问题了:很显 ...

  4. django 1.10 CSRF验证失败的解决过程

    最近工作闲,没事自学django,感觉这个最烦的就是各版本提供的api函数经常有变化,不是取消了就是参数没有了,网上搜到的帖子也没说明用的是什么版本的django,所以经常出现搬运过来的代码解决不了问 ...

  5. django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方式

    现象 表单界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29sbG9yNTI1/font/5a6L5L2T/fontsize/400/fi ...

  6. 解决“<form>提交,CSRF验证失败。请求中止”

    在相应的提交页面,内,添加"{%csrf_token%} ",如: Django工程的app文件的views.py添加屏蔽装饰器,(相应的函数上): from django.vie ...

  7. Python菜鸟之路:Django CSRF跨站请求伪造

    前言 CSRF,Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对 ...

  8. vue-resource pos提交t数据时碰到Django csrf

    最近在用Vue写前端代码,再用vue-resource向后台提交数据.项目后台是用python+Django开发的.下面我就复盘一下我出现问题的经过. 首先,想用vue进行数据交互只能引入vue-re ...

  9. pos提交提交数据时碰到Django csrf

    我的github(PS:希望star):https://github.com/thWinterSun/v-admin 最近在用Vue写前端代码,再用vue-resource向后台提交数据.项目后台是用 ...

随机推荐

  1. NC14247 Xorto

    NC14247 Xorto 题目 题目描述 给定一个长度为 \(n\) 的整数数组,问有多少对互不重叠的非空区间,使得两个区间内的数的异或和为 \(0\) . 输入描述 第一行一个数 \(n\) 表示 ...

  2. 关于 k 进制线性基

    本质还是高斯消元,使其成为上三角矩阵.但是 \(k\) 不一定是质数. 但我们不需要保证已有数字不改变,只要维护的是一个上三角矩阵就行.所以我们可以利用更相减损让其中一个向量的最高位 \(= 0\) ...

  3. Selenium指定浏览器路径

    ChromeOptions options = new ChromeOptions(); options.setBinary("C:\\Program Files (x86)\\Google ...

  4. Linux 更改家目录下的目录为英文

    export LANG=en_US xdg-user-dirs-gtk-update

  5. mybatis-拦截器实际应用-替换表名-2022新项目

    一.业务场景 考虑到新项目中部分与业务数据相关的表在后期数据量会比较大,架构师在最开始设计项目中与业务数据相关的表时,就已经考虑使用分表来 进行处理,给业务数据相关的每张表都添加统一批次的后缀,查询这 ...

  6. Ask.com用过什么名字?

    搜索引擎 Ask.com 曾是美国第三,世界第六大公网搜索引擎,仅次于 Google 搜索.Bing 和百度.NAVER.Yandex. Ask.com 曾经用过什么名字? Ask Jetson As ...

  7. qbxt数学五一Day4

    目录 1. 随机试验 2. 概率 1. 平凡 2. 条件概率 3. 期望 习题 1 2 3 4 1. 随机试验 定义: 不能预先确知结果 试验之前可以预测所有可能结果或范围 可以在相同条件下重复实验 ...

  8. YII的延迟加载

    类的映射表 use app\model\order \Yii::$classMap['app\models\Order'] = "D:\wamp\www\...\models\Order.p ...

  9. C# 发送Http请求,传文件和其他参数

    /// <summary> /// httpWebRequest post by dic /// </summary> /// <param name="url ...

  10. vue脚手架创建项目后使用路由报错Object(...) is not a function问题

    在这之前我做过的vue项目没有这种问题,今天突然出现这个问题,也检查了很久的代码,最后解决我也不知道我是哪一步做错了 首先我是创建的vue2项目,基本操作跟平常一样,在运用路由跳转的时候遇到这个问题 ...