现象:

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."的解决方法.的更多相关文章

  1. 关于 DjangoUeditor 上传图片图片失败,csrf token missing or incorrect 的解决办法

    Forbidden (CSRF token missing or incorrect.): /ueditor/controller/ [27/Jun/2017 23:49:25] "POST ...

  2. Forbidden (CSRF token missing or incorrect.):错误解决办法

    在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置, ...

  3. 关于django1.7.7使用ajax后出现“CSRF token missing or incorrect”问题的解决办法

    最近使用Python3.3.25和django1.7.7开发公司项目,在使用ajax来post数据时,居然一直提示:403错误,原因是“CSRF token missing or incorrect” ...

  4. django CSRF token missing or incorrect

    django 异步请求时提示403 按照一般情况权限问题,python文件没有问题,仔细看了下response里有一句 CSRF token missing or incorrect.这个肯定是因为安 ...

  5. 对于 前端请求Django 后端服务出现403 Forbidden (CSRF token missing or incorrect.) 问题的解析

    Django中使用ajax post向後臺傳送資料時403 Forbidden (CSRF token missing or incorrect.):的解決辦法 在Django中使用ajax post ...

  6. django rest framework csrf failed csrf token missing or incorrect

    django rest framework csrf failed csrf token missing or incorrect REST_FRAMEWORK = { 'DEFAULT_AUTHEN ...

  7. django前后端分离403 csrf token missing or incorrect

    根据这个链接https://stackoverflow.com/a/26639895 这是一个django的跨域访问问题. django,会对合法的跨域访问做这样的检验,cookies里面存储的'cs ...

  8. ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)

    由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_pag ...

  9. django中使用POST方法 使用ajax后出现“CSRF token missing or incorrect”

    这个是因为在django的ajax中默认添加了token,因此需要在cookie中增加token头信息. 首先使用JavaScript函数获取token: function getCookie(nam ...

随机推荐

  1. Windows下安装MySQL5.6绿色版

    建议安装MySQL绿色版的,什么是绿色版的?就是免安装,下载下来的截图是这样的 在该目录下创建一个文件夹/data用于存放数据, 新建一个my.ini文件,my.ini里面最基本的配置如下,my.in ...

  2. WCF Rest post请求

    只有遇到问题才能解决问题,这个问题之前也遇到过因为没有及时总结所以这个问题又重新出现困扰了我8个小时. ajax 请示WCF Rest 是一个比较成熟的技术.所以 涉及到了几个技术点 1 jquery ...

  3. rabbitmq kafka storm

    rabbitmq:实时消息传递 kafka:消息的持久化 storm:使用拓扑逻辑进行

  4. 什么叫强类型的DATASET ?对DATASET的操作处理?强类型DataSet的使用简明教程

    强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...

  5. 吃CPU的openmp 程序

    g++ -o eat -fopenmp eat.cpp #include "stdio.h" int main(int argc, char *argv[]) { #pragma ...

  6. windows 操作系统问题的解决

    浏览器之间具有较大的差异: 如果某问题只存在于某浏览器,而其他浏览器不存在此问题,则说明,问题出在该浏览器上: 或者是缓存已满:⇒ 清除缓存: 或者浏览器的兼容性问题: 1. error 1327. ...

  7. I.MX6 android mkuserimg.sh

    /************************************************************************** * I.MX6 android mkuserim ...

  8. bzoj 3991 寻宝游戏

    题目大意: 一颗树 有一个点的集合 对于每个集合的答案为 从集合内一个点遍历集合内所有点再返回的距离最小值 每次可以选择一个点 若在集合外便加入集合 若在集合内就删除 求每次操作后这个集合的答案 思路 ...

  9. bzoj 3609: [Heoi2014]人人尽说江南好【博弈论】

    参考:https://blog.csdn.net/Izumi_Hanako/article/details/80189596 胜负和操作次数有关,先手胜为奇,所以先手期望奇数后手期望偶数,最后一定能达 ...

  10. spoj 839 OPTM - Optimal Marks&&bzoj 2400【最小割】

    因为是异或运算,所以考虑对每一位操作.对于所有已知mark的点,mark的当前位为1则连接(s,i,inf),否则连(i,t,inf),然后其他的边按照原图连(u,v,1),(v,u,1),跑最大流求 ...