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向后台提交数据.项目后台是用 ...
随机推荐
- VS Code 调教日记(2022.6.26更新)
VS Code 调教日记(2022.6.26更新) 基于msys2的MinGW-w64 GCC的环境配置 下载并安装msys2 到路径...msys2安装路径...\msys64\etc\pacman ...
- freeswitch拨打分机号源代码跟踪
概述 freeswitch是一款非常好用的开源VOIP软交换平台. 之前我们有介绍过使用fs拨打分机号的方法,其中代码流程是比较复杂的,所以单独开一章介绍. fs拨打分机号,是使用send_dtmf接 ...
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
Cascade-LSTM是一个用于虚假信息级联检测的树结构神经分类器,它本质上是一个谣言(假新闻)检测模型,它将谣言检测任务视为一个树分类问题. Cascade-LSTM在递归神经网络(本文具体基于T ...
- SpringMVC指定配置文件位置和名称,控制Servlet的加载时间
1. 2.
- 基于YCbCr色彩模型的简易肤色识别器
一.实验方法 实验共选取了12张图像,利用画笔工具在每幅图像上选取5个点,并分别记录RGB值.取点方式如下图所示: 总共70个点,R,G,B的值分别如下表所示: RGB色彩模型和YCbC ...
- Test_day01月_总结
1)Object是所有类的超类,在java.lang包中 2)标识符命名规则 3)八种基本数据类型有哪些?每种类型所占的字节数? 整数直接量默认为int类型 浮点数直接量默认为double类型 4)字 ...
- 数据质量管理工具预研——Griffin VS Deequ VS Great expectations VS Qualitis
开源数据质量管理工具预研--Griffin VS Deequ VS Great expectations VS Qualitis. 概述 数据质量监控(DQC)是最近很火的一个话题,也是数据治理中 ...
- 21条最佳实践,全面保障 GitHub 使用安全
GitHub 是开发人员工作流程中不可或缺的一部分.无论你去哪个企业或开发团队,GitHub 都以某种形式存在.它被超过8300万开发人员,400万个组织和托管超过2亿个存储库使用.GitHub 是世 ...
- CF1705A Mark the Photographer 题解
题意: 给定一队人的身高,将其分成两队,问能否实现前面的人均低于后面的人至少 \(x\) 个单位长度. 做法: 将这队人的身高进行排序,\(h_1 \sim h_n\) 即为第一队,\(h_{n+1} ...
- 基于EasyExcel的大数据量导入并去重
源码:https://gitee.com/antia11/excel-data-import-demo 背景:客户需要每周会将上传一个 Excel 数据文件,数据量单次为 20W 以上,作为其他模块和 ...