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

原文地址:https://www.cnblogs.com/zhujiabin/p/8260288.html

时间: 2024-10-20 10:55:43

"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 /ueditor/controller/?imagePathFormat=courses%2Fueditor%2F&filePathFormat=courses%2Fueditor%2F&action=uploadvideo&encode=utf-8 HTTP/1.1" 4

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

在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置,补上以下代码即可: 1 <script> 2 $.ajaxSetup({ 3 data:{csrfmiddlewaretoken:'{{ csrf_token }}'} 4 }) 5 </script> 原文地址:https://www.cnblogs.com/lhb-alan/p/1

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

由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_page' ret.uut_id %}", { // uut_id: "{{ ret.uut_id }}", filter: JSON.stringify(filters), }, function(data){ var retData = JSON.parse(data); cons

解决django出现CSRF token missing or incorrect.错误

给template中的每个POST form添加{% csrf_token %}标签. <form action="/contact/" method="post">     {% csrf_token %}     <p>Subject: <input type="text" name="subject"></p>     <p>Your e-mail (optio

Forbidden (CSRF token missing or incorrect.):

CSRF令牌失效或丢失,Ajax请求页面报错(403 Forbidden ) csrftoken存在 页面响应为CSRF验证失败请求被中断,经过测试,该错误并非是没有在表单中加入{% csrf_token%} 导致[试过] 刷新dns缓存依然无法解决该问题, 解决: 检查setting配置中 'django.middleware.csrf.CsrfViewMiddleware', 在浏览器隐私中清除cookie,[无效] 刷新dns[无效] 最后解决方案: 在引用js之前加入Ajax启动项,之前

djangopost请求报错:Forbidden (CSRF token missing or incorrect.)

解决办法:引入csrf_exempt包: from django.views.decorators.csrf import csrf_exempt 然后增加装饰器: @csrf_exempt def register(request): request.encoding='utf-8' if request.GET: return render(request,'register.html') elif request.POST: dictData = request.POST context

django出错:CSRF token missing or incorrect.

解决方案: 1.在对应的模板中, 为每个POST的form添加一行代码:{% csrf_token %}. 如下所示: <form action = "" method = "post"> {% csrf_token %} <table> {{ form.as_table }} </table> <input type = "submit" value = "Submit"> &

django rest framework csrf failed csrf token missing or incorrect

REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',)} or MIDDLEWARE_CLASSES = ( 'store.disable.DisableCSRF',) disable.py class DisableCSRF(object): def process_request(self, request):        seta

Multiple markers at this line - Missing semicolon时的解决方法

Myeclipse的web项目中的js文件报Multiple markers at this line - Missing semicolon时的解决方法 MyEclipse的web项目中的js文件报Multiple markers at this line - Missing semicolon时,是由于还没有告诉myeclipse去验证它.解决方法,选中js文件,右键Myeclipse--ManaValidation--ExcludeResource--(选中全部或者报错的那个js)--点击