Django CSRF验证失败. 请求被中断.
当页面中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
requestto the template'srendermethod.- 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 usecsrf_protecton any views that use thecsrf_tokentemplate 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 haveDEBUG = Truein your Django settings file. Change that toFalse, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
在网上查找了很多资料,普遍采用的解决方法是:
检查
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',
)html中的form添加模板标签{% csrf_token %}
<form action="/saveBlog/" method="post">
{% csrf_token %}
...
</form>针对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)
因为官方文档上说render是render_to_response的shortcut,所以没有照网站资料上说的去改。于是错误总是无法解决。
几番挣扎之后,终于在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验证失败. 请求被中断.的更多相关文章
- Django项目报错: 禁止访问(403),CSRF验证失败,相应中断
如果想要取消表单的CSRF防护,可以在模板上删除{% csrf_token %}, 并且在相应的视图函数中添加装饰器@csrf_exempt, 代码如下: from django.views.deco ...
- Django在form提交CSRF验证失败. 相应中断问题
CSRF验证失败. 相应中断. 1).首先,我们可以先看一下出现问题的所在的原因. Your browser is accepting cookies. The view function passe ...
- Django中ajax发送post请求,报403错误CSRF验证失败解决办法
今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败:先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了CSRF的问题了:很显 ...
- django 1.10 CSRF验证失败的解决过程
最近工作闲,没事自学django,感觉这个最烦的就是各版本提供的api函数经常有变化,不是取消了就是参数没有了,网上搜到的帖子也没说明用的是什么版本的django,所以经常出现搬运过来的代码解决不了问 ...
- django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方式
现象 表单界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29sbG9yNTI1/font/5a6L5L2T/fontsize/400/fi ...
- 解决“<form>提交,CSRF验证失败。请求中止”
在相应的提交页面,内,添加"{%csrf_token%} ",如: Django工程的app文件的views.py添加屏蔽装饰器,(相应的函数上): from django.vie ...
- Python菜鸟之路:Django CSRF跨站请求伪造
前言 CSRF,Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对 ...
- vue-resource pos提交t数据时碰到Django csrf
最近在用Vue写前端代码,再用vue-resource向后台提交数据.项目后台是用python+Django开发的.下面我就复盘一下我出现问题的经过. 首先,想用vue进行数据交互只能引入vue-re ...
- pos提交提交数据时碰到Django csrf
我的github(PS:希望star):https://github.com/thWinterSun/v-admin 最近在用Vue写前端代码,再用vue-resource向后台提交数据.项目后台是用 ...
随机推荐
- 网络通讯之Socket-Tcp(一)
网络通讯之Socket-Tcp 分成3部分讲解: 网络通讯之Socket-Tcp(一): 1.如何理解Socket 2.Socket通信重要函数 网络通讯之Socket-Tcp(二): 1.简单So ...
- FastASR——PaddleSpeech的C++实现
FastASR 基于PaddleSpeech所使用的conformer模型,使用C++的高效实现模型推理,在树莓派4B等ARM平台运行也可流畅运行. 项目简介 本项目仅实现了PaddleSpeech ...
- CMU15445 (Fall 2019) 之 Project#4 - Logging & Recovery 详解
前言 这是 Fall 2019 的最后一个实验,要求我们实现预写式日志.系统恢复和存档点功能,这三个功能分别对应三个类 LogManager.LogRecovery 和 CheckpointManag ...
- 不是第七代的 Win 7
贡献者:历史上的今天 Windows 7 是由微软公司(Microsoft)2009 年 10 月 22 日发布的桌面端操作系统,它影响了每个行业的方方面面,以至于很多人仍然在日常生活和工作中使用它. ...
- C#/VB.NET: 将Word或Excel文档转化为Text
Text文件只由纯文本内容组成,且没有格式,所以其大小比Word或Excel文件更小.除此之外,Text文件还具有跨平台性,几乎与所有应用程序都兼容.因此,在某些时候,我们可能需要将Word或Exce ...
- java------JRE和JDK
JDK(Java Development kit):Java开发工具包 包括 JVM(Java Virtual Machine):java虚拟机,真正运行java程序的地方(Java语言在运行时并不是 ...
- C#基础-面向对象详解
面向对象详解 一.什么是面向对象 1>面向对象是一种程序设计思想 2>面向过程和面向对象是什么? 例如要把大象放冰箱怎么做? 面向过程:打开冰箱门->把大象扔进去->关上冰箱门 ...
- openstack 虚拟机网卡被重名为cirename0
虚拟机网卡被重名为cirename0 在虚拟机挂载多网卡情况下,你在虚拟机上卸载网卡后,再创建新的port挂给虚拟机使用,如果虚拟机不经过重启的话,是不会有任何问题的.但是,如果虚拟机重启了,你 ...
- YII behaviors使用
文件 frontend/libs/FilterTest.php <?php /** * Created by PhpStorm. * Date: 2016/5/27 * Time: 14:16 ...
- appache ab测试高并发
ab使用范例: 要执行 1000 次的 connection, 20 次的 concurrent (并行, 同时): 语法: ab -n 1000 -c 20 http://localhost/ind ...