"CSRF token missing or incorrect."的解决办法怎么解决

2025年03月13日 03:18
有3个网友回答
网友(1):

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 %}

{% 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):
#...

网友(2):

1、首先需要安装pip。

下载get-pip.py文件,网址是:https://bootstrap.pypa.io/get-pip.py。

使用 python get-pip.py 命令自动安装。

2、然后使用pip安装需要的东西。

pip install kombupip install celerypip install django-celery

3、最后安装erlang 和 RabbitMQ。

windows系统直接百度下载,安装后自动开启服务。

具体如下:

1、简介

Linux操作系统是基于UNIX操作系统发展而来的一种克隆系统,它诞生于1991 年的 [Linux桌面] 10 月5 日(这是第一次正式向外公布的时间)。以后借助于Internet网络,并通过全世界各地计算机爱好者的共同努力,已成为今天世界上使用最多的一种UNIX 类操作系统,并且使用人数还在迅猛增长。

2、基本信息

Linux[2]操作系统是UNIX操作系统的一种克隆系统,它诞生linux系统于1991 年的10 月5 日(这是第一次正式向外公布的时间)。以后借助于Internet网络,并通过全世界各地计算机爱好者的共同努力,已成为今天世界上使用最多的一种UNIX 类操作系统,并且使用人数还在迅猛增长。

3、分区规定

设备管理在 Linux 中,每一个硬件设备都映射到一个系统的文件,对于硬盘、光驱等,IDE 或 SCSI 设备也不例外。Linux 把各种 IDE 设备分配了一个由 hd 前缀组成的文件;而对于各种 SCSI 设备,则分配了一个由 sd 前缀组成的文件。

网友(3):

来到这个页面的朋友可能大多是使用RequestContext(request)来生成csrf_token 以指望摆脱Forbidden(403)的吧?恭喜你,来对了地方,

在花了一天时间利用远古教学视频学习Django的时候遇到的问题。

其根本原因非常的……简单:Django更新了

现在的写法:

return render_to_response('XX.html',context=csrf(request))

如果还想传参怎么办!?(因为context已经被占用了)。很简单:

  • c=csrf(request)  

  • c.update({'msg':'nooooooooo'})  

  • return render_to_response('xxx.html',context=c)#或者render版本的  

这样,你的模板里就可以收到 msg了

------特别申明,摘抄至PlusZhang的博客----

网页链接