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
request
to the template'srender
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 usecsrf_protect
on any views that use thecsrf_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 haveDEBUG = True
in 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 render
method.(即:视图函数中传递 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向后台提交数据.项目后台是用 ...
随机推荐
- 项目中使用@Transactional需要注意的点
项目如果是Spring Boot.或者Spring Cloud,切记需要在启动类上加入@EnableTransactionManagement该注解.否则事务不生效. @Transactional是一 ...
- 使用APICloud AVM框架开发预约应用
前段时间跟朋友一起搞了一个预约的项目,前端用的APICloud的AVM框架做的,后端用的php开发的,用的tp5框架,没几天就搞出来了.简单跟大家分享一下开发中的一些功能点的实现吧.也欢迎大家一起探讨 ...
- error: cannot open .git/FETCH_HEAD: Permission denied
可能原因:该操作的执行者对该目录没有写权限 解决:1.类Unix平台,使用chown将目录改为自己: 2.Windows平台,取消只读选项,给everyone用户所有权限:
- Dokcer运行Nacos容器自动退出问题
Dokcer运行Nacos容器自动退出问题 参考博文 学生党,租的云服务器,2核2G.使用Docker运行Nacos容器的时候发现总是自动退出.Nacos日志里面没有明显的报错信息.查了一下是内存溢出 ...
- 5.1 从C到C++
在前4章中介绍了C语言的主要内容,已经足以应付许多算法竞赛的题目了,然而能写不代表好写,有些虽然能够用C语言实现,但是使用C++写起来往往会更快,并且不容易出错 从c到c++ C语言是一门很有用的语言 ...
- nginx编译安装支持lua脚本
一.准备编译环境 1.操作系统:CentOS7.6 2.安装编译所需安装包 yum install gcc pcre pcre-devel zlib zlib-devel openssl openss ...
- linux学习随笔3之linux安全
1.history显示时间,用户和命令 vim /etc/profile export HISTTIMEFORMAT=" %F %T `who -u am i 2>/dev/null| ...
- 一篇文章带你搞定BFC~
一.什么是BFC 是 Block Formatting Contexts 的缩写,名为"块级格式化上下文". 是指浏览器中创建了一个独立的渲染区域,并且拥有一套渲染规则,他决定了其 ...
- V8中的快慢属性(图文分解更易理解)
出于好奇:js中使用json存数据查找速度快,还是使用数组存数据查找快? 探究V8中对象的实现原理,熟悉数组索引属性.命名属性.对象内属性.隐藏类.描述符数组.快慢属性等等. D8调试工具使用请来这里 ...
- 4步教你学会使用Linux-Audit工具
摘要:简单来讲audit是Linux上的审计工具,可以用来记录和监控对文件.目录.系统资源的更改:Audit无法直接增强系统的安全性,但是它可以用于发现违反系统安全政策的行为. 本文分享自华为云社区& ...