当页面中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. 循环结构-for循环和while循环

    循环语句1--for for循环语句格式: for(初始化表达式①; 布尔表达式②; 步进表达式④){ 循环体③ } 执行流程 执行顺序:①②③④>②③④>②③④-②不满足为止. ①负责完 ...

  2. Note -「模拟退火」

    随机化算法属于省选芝士体系 0x01 前置芝士 你只需要会 rand 就可以啦! 当然如果你想理解的更透彻也可以先看看 爬山算法 0x02 关于退火 退火是一种金属热处理工艺,指的是将金属缓慢加热到一 ...

  3. CD 从抓轨到搭建流媒体服务器 —— 以《月临寐乡》为例

    2022-07-19 v0.0.1 由于某些原因,进了 Static World 的群并入坑了 月临寐乡 ,梦开始了.作为幻想乡的新人,也算是有了自己喜欢的社团.但是更细节的东西,狐狐脑子一下子塞不下 ...

  4. Nginx搭建简易文件服务器

    Nginx搭建简易文件服务器 1.安装nginx,此处略过 2.修改nginx配置文件 详细如下 # 此处为部分文件是否有权限,使用root,则不会出现403权限问题 user root; worke ...

  5. 重写并自定义依赖的原生的Bean方法

    转载请注明出处: 在项目开发过程中,往往是直接应用很多jar包中依赖且声明好的Bean,拿来即用,但很多场景也需要对这些原生的Bean 进行自定义,定制化封装,这样在项目使用的过程中,可以使用自定义的 ...

  6. Docker默认桥接网络是如何工作的

    1. 启动一个Docker容器 一般来说,我们起一个容器比如一个简单的nginx服务会向这样 docker run -d --rm nginx:XXX OK容器起来了,但是并不能通过宿主机被外面的机器 ...

  7. mysql查询版本

    系统环境下 :mysql -V; mysql内:select version();

  8. odoo14 入门解刨关联字段

    Odoo中关联字段是用来绑定表与表之间主从关系的. 主从关系指: 首先必须要明白id的存在的意义,它具备"唯一"的属性,也就是表中所有记录中该字段的值不会重复. 假设表A存储是身份 ...

  9. React报错之`value` prop on `input` should not be null

    正文从这开始~ 总览 当我们把一个input的初始值设置为null或者覆盖初始值设置为null时,会产生"valueprop on input should not be null" ...

  10. 停止、启动nginx以及在windows下使用dos命令停止占用的端口

    windows下使用dos命令查看占用端口号并停止 windows+R打开命令窗口 查找占用端口对应的PID(进程号) netstat -ano|findstr "port" # ...