当页面中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. VScode中配置Java环境

    vscode 中配置Java环境 转载说明:本篇文档原作者[@火星动力猿],文档出处来自哔哩哔哩-[教程]VScode中配置Java运行环境 转载请在开头或显眼位置标注转载信息. 1.下载VScode ...

  2. CentOS7使用LVM缩减/home空间,扩大/空间

    CentOS7使用LVM缩减/home空间,扩大/空间方法:把/home里的内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/文件系统.新建/home,恢复/home的原内容1.查看默认分区[ ...

  3. github package的使用教程

    一.写在前面 上一次,笔者向大家介绍了把gitlab仓库作为npm私包的使用方法,具体的详见我的博文地址https://www.cnblogs.com/cnroadbridge/p/16406476. ...

  4. java枚举和注解

    枚举 一.枚举(enumeration) 是一组常量的集合,可以理解为:枚举属于一种特殊的类,里面只包含一组有限的特定的对象,构造方法默认为private. 二.枚举的两种实现方式 1.自定义实现枚举 ...

  5. C++20 以 Bazel & Clang 开始

    C++20 如何以 Bazel & Clang 进行构建呢? 本文将介绍: Bazel 构建系统的安装 LLVM 编译系统的安装 Clang is an "LLVM native&q ...

  6. SpringBoot接口 - 如何生成接口文档之非侵入方式(通过注释生成)Smart-Doc?

    通过Swagger系列可以快速生成API文档,但是这种API文档生成是需要在接口上添加注解等,这表明这是一种侵入式方式: 那么有没有非侵入式方式呢, 比如通过注释生成文档? 本文主要介绍非侵入式的方式 ...

  7. 网络营销谁在行?PHP小哥打个样

    PHP -ゞ 阿白同学的学习笔记 PHP学习笔记 - 01 - web2.0 - 网络营销 @ 目录 一. 前言 二. 开始(借助菜鸟教程平台练习) 1. Hello World -- 第一个案例 2 ...

  8. JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue

    一丶Condition 1.概述 任何一个java对象都拥有一组定义在Object中的监视器方法--wait(),wait(long timeout),notify(),和notifyAll()方法, ...

  9. 钡铼BL102分布式IO系统如何应用于锂电池行业

    近年来,全球新能源汽车的蓬勃发展促进了锂电池行业的发展.随着锂电池标准化程度的提高,电池和模块规格的标准化是未来的发展趋势,也促进了自动化模块生产线的发展. 锂电池模块生产线通过涂胶-电池堆叠-组装- ...

  10. linux学习随笔2之防火墙

    centos7默认使用的防火墙是firewalld 查看所有打开的端口: firewall-cmd --zone=public --list-ports 更新防火墙规则: firewall-cmd - ...