"CSRF token missing or incorrect."的解决方法.
现象:
Forbidden (403)
CSRF verification failed. Request aborted.
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 uses RequestContext for the template, instead of Context.
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.
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〉django工程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',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
2〉html中的form添加模板标签{% csrf_token %}
<form action="." method="post">{% csrf_token %}
3〉django工程views.py
from django.shortcuts import render_to_response
from django.template import RequestContext def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
有疑问请戳Cross Site Request Forgery protection
P.S如果要屏蔽CSRF
方法1:注释掉django工程settings.py中
#'django.middleware.csrf.CsrfViewMiddleware'
方法2:django工程views.py添加屏蔽装饰器
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def some_view(request):
#...
"CSRF token missing or incorrect."的解决方法.的更多相关文章
- 关于 DjangoUeditor 上传图片图片失败,csrf token missing or incorrect 的解决办法
Forbidden (CSRF token missing or incorrect.): /ueditor/controller/ [27/Jun/2017 23:49:25] "POST ...
- Forbidden (CSRF token missing or incorrect.):错误解决办法
在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置, ...
- 关于django1.7.7使用ajax后出现“CSRF token missing or incorrect”问题的解决办法
最近使用Python3.3.25和django1.7.7开发公司项目,在使用ajax来post数据时,居然一直提示:403错误,原因是“CSRF token missing or incorrect” ...
- django CSRF token missing or incorrect
django 异步请求时提示403 按照一般情况权限问题,python文件没有问题,仔细看了下response里有一句 CSRF token missing or incorrect.这个肯定是因为安 ...
- 对于 前端请求Django 后端服务出现403 Forbidden (CSRF token missing or incorrect.) 问题的解析
Django中使用ajax post向後臺傳送資料時403 Forbidden (CSRF token missing or incorrect.):的解決辦法 在Django中使用ajax post ...
- django rest framework csrf failed csrf token missing or incorrect
django rest framework csrf failed csrf token missing or incorrect REST_FRAMEWORK = { 'DEFAULT_AUTHEN ...
- django前后端分离403 csrf token missing or incorrect
根据这个链接https://stackoverflow.com/a/26639895 这是一个django的跨域访问问题. django,会对合法的跨域访问做这样的检验,cookies里面存储的'cs ...
- ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)
由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_pag ...
- django中使用POST方法 使用ajax后出现“CSRF token missing or incorrect”
这个是因为在django的ajax中默认添加了token,因此需要在cookie中增加token头信息. 首先使用JavaScript函数获取token: function getCookie(nam ...
随机推荐
- C++手稿:std::string
字符串在非常多编程语言中已经成为基本数据类型,C语言中我们使用char*来手动申请和维护字符串, 在C++中,能够使用std::string来方便地创建和操作字符串. string是一个模板类.它有b ...
- 嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析
#define SBULL_MINORS 16 /* 每个sbull设备所支持的次设备号的数量 */ #define KERNEL_SECTOR_SIZE 512 // 本地定 ...
- Chapter1-data access reloaded:Entity Framework(下)
1.4 Delving deep into object/relational differences 深入挖掘对象关系的不同 理解面向对象和关系世界的不同是重要的,因为他会影响你设计一个对象模型或者 ...
- 2016/3/16 45道MySQL 查询练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- easyUI下拉列表点击事件的使用
可以通过input 和select来创建下拉列表 其中select的创建如下: 通过json来创建js数组 [{ "id":1, "text":"te ...
- 用 kGDB 调试 Linux 内核
简介 这个文档记录了用kGDB调试Linux内核的全过程,都是在前人工作基础上的一些总结.以下操作都是基于特定板子来进行,但是大部分都能应用于其他平台. 要使用KGDB来调试内核,首先需要修改conf ...
- GrideView(二)---删除功能
情景一. 没有外键关联, 操作:在数据源中将删除选项选中--- GrideView 中的删除 选项选中 即可情景二. 有外键关联 *RowDeleting 行删除前触发 *RowDeleted 行删除 ...
- 织梦仿站列表页pagelist分页显示竖排,如何修改成横排?
织梦仿站列表页pagelist分页显示竖排,如何修改成横排? 织梦列表页的分页标签是采用pagelist来进行调用的,但是很多人在调用之后会出现一个列表竖着排列的问题(横排美观度好一些),还是非常不美 ...
- java修改linux文件
package vedio.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fil ...
- CentOS6.0忘记root密码解决办法
说明操作系统:CentOS 6.0遇到问题:忘记管理员账号root的密码,进不了系统解决办法:重置root密码为123456操作: 开机启动系统,在进入系统之前按键盘上面的Esc键,会进入下面的界面 ...