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向后台提交数据.项目后台是用 ...
随机推荐
- vue Blob 下载附件报错
vue Blob 下载附件报错,不妨试试: window.location.href=后台地址
- 项目git commit时卡主不良代码:husky让Git检查代码规范化工作
看完 <前端规范之Git工作流规范(Husky + Commitlint + Lint-staged) https://www.cnblogs.com/Yellow-ice/p/15349873 ...
- C++实现ETW进行进程变动监控
C++实现ETW进行进程变动监控 文章地址:https://www.cnblogs.com/Icys/p/EtwProcess.html 何为Etw ETW(Event Tracing for Win ...
- Java 中的对象池实现
点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 最近在 ...
- 分析 java.util.Hashtable 源码
概述 基于J11,该类已经淘汰,如果使用线程安全的则用 ConcurrentHashMap ,用线程不安全的则使用 HashMap .仅与HashMap进行比较 结构以及依赖关系 HashTable ...
- AOV网的实现(数据结构)
#include <stdio.h> #include <stdlib.h> #include <string.h>//我这里的头以及尾巴与书上的不一样. int ...
- python jinjia2 使用语法
简介 对于jinjia2来说,模板仅仅是文本文件,可以生成任何基于文本的文件格式,例如HTML.XML.CSV.LaTex 等等,以下是基础的模板内容: <!DOCTYPE html> & ...
- 6.14 YZBOI模拟赛solution
\(6.14\ YZBOI\)模拟赛\(solution\) 本来不想写题解来着...毕竟是自己找的题还是写一写吧 上午为了整活,就把赛制改成\(IOI\)赛制了,于是乎拯救了大家的\(70pts\) ...
- Spring的简单使用(1)
一:IOC(控制反转):它是由spring容器进行对象的创建和依赖注入,程序员使用时直接取出即可 正转:例如: Student stu=new Student(): stu.setname(" ...
- 不止跑路,拯救误操作rm -rf /*的小伙儿
摘要:误执行了 rm -rf /* 之后,除了跑路还能怎么办? 本文分享自华为云社区<拯救被 rm -rf 伤到的小伙>,作者:Gauss 松鼠会. 灵魂画师再次上线 在开饭前我们先了 ...