CSRF verification failed. Request aborted.

简介:

使用django提交form表单时,提交方式为post,总是提交不成功,报错如下:


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.



虽然按照上面所说的方法:

第一种:在表单里加上{% csrf_token %}就行了。
第二种方法是在Settings里的MIDDLEWARE_CLASSES增加配置:
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.csrf.CsrfResponseMiddleware',


都加上了,但是并没有什么用,网上查资料才找到答案,从view到页面时候,我是酱紫的:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from  django.shortcuts  import  *
from  contact.forms  import  ContactForm
 
def  contact(request):
     if  request.method = = 'POST' :
         form =  ContactForm(request.POST)
         if  form.is_valid():
             cd  =  form.cleaned_data
             send_mail(
                 cd[ 'subject' ],
                 cd[ 'message' ],
                 cd.get( 'email' 'noreply@example.com' ),
                 [ 'siteowner@example.com' ],
             )
             return  HttpResponseRedirect( '/contact/thanks/' )
     else :
         form = ContactForm()
     return  render_to_response( 'contact_form.html' , { 'form' : form})

首先request没有post方法,进入else,显示的为contact_form.html页面,此时报错,网上有一种说法,必须要使用RequestContext对象,

1
return  render_to_response( 'contact_form.html' , { 'form' : form},context_instance = RequestContext(request))


将else中的语句加入RequestContext后,成功!






      本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1678395,如需转载请自行联系原作者


相关文章
Detail about CSRF token fetch request
Created by Jerry Wang, last modified on Jun 17, 2015 updateSecurityToken bTokenHandling x-csrf-token oServiceData.securityToken ODataModel._createBatchRequest ODataModel.prototype.refreshSecurityToken
Detail about CSRF token fetch request
|
安全 Java Spring
What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security
18.5.1 Timeouts One issue is that the expected CSRF token is stored in the HttpSession, so as soon as the HttpSession expires your configured AccessD...
3119 0
|
Python
Django 403错误:CSRF verification failed. Request aborted
网上有解决办法,我自己的组合是: 一,FORM加标识 {% csrf_token %} 类型: 二,VIEW导入Redirect from django.http import HttpResponsefrom django.http import HttpResponseRedirectfrom django.shortcuts import render 三,注意SETTING里我加   'django.middleware.csrf.CsrfResponseMiddleware',反而出错,只留一条即好。
1118 0
|
5月前
|
JavaScript 安全 前端开发
js开发:请解释什么是XSS攻击和CSRF攻击,并说明如何防范这些攻击。
XSS和CSRF是两种常见的Web安全威胁。XSS攻击通过注入恶意脚本盗取用户信息或控制账户,防范措施包括输入验证、内容编码、HTTPOnly Cookie和CSP。CSRF攻击则诱使用户执行未经授权操作,防范手段有CSRF Tokens、双重验证、Referer检查和SameSite Cookie属性。开发者应采取这些防御措施并定期进行安全审计以增强应用安全性。
101 0
|
11月前
|
安全 NoSQL Java
互联网并发与安全系列教程(06) - 常见的Web安全漏洞(CSRF攻击)
互联网并发与安全系列教程(06) - 常见的Web安全漏洞(CSRF攻击)
107 0
|
12月前
|
SQL 安全 前端开发
渗透攻击实例-邪恶的CSRF(社会工程学)
渗透攻击实例-邪恶的CSRF(社会工程学)
|
5月前
|
缓存 安全 JavaScript
前端安全:Vue应用中防范XSS和CSRF攻击
【4月更文挑战第23天】本文探讨了在Vue应用中防范XSS和CSRF攻击的重要性。XSS攻击通过注入恶意脚本威胁用户数据,而CSRF则利用用户身份发起非授权请求。防范措施包括:对输入内容转义、使用CSP、选择安全的库;采用Anti-CSRF令牌、同源策略和POST请求对抗CSRF;并实施代码审查、更新依赖及教育团队成员。通过这些实践,可提升Vue应用的安全性,抵御潜在攻击。
798 0
|
3月前
|
SQL 安全 数据库
Python Web开发者必学:SQL注入、XSS、CSRF攻击与防御实战演练!
【7月更文挑战第26天】在 Python Web 开发中, 安全性至关重要。本文聚焦 SQL 注入、XSS 和 CSRF 这三大安全威胁,提供实战防御策略。SQL 注入可通过参数化查询和 ORM 框架来防范;XSS 则需 HTML 转义用户输入与实施 CSP;CSRF 防御依赖 CSRF 令牌和双重提交 Cookie。掌握这些技巧,能有效加固 Web 应用的安全防线。安全是持续的过程,需贯穿开发始终。
71 1
Python Web开发者必学:SQL注入、XSS、CSRF攻击与防御实战演练!
|
2月前
|
Web App开发 存储 安全
就一次!带你彻底搞懂CSRF攻击与防御
与XSS攻击相比,利用CSRF漏洞发动攻击会比较困难,这也是在网络上看起来CSRF的人气小于XSS的原因之一。下面我们来利用CSRF漏洞发起攻击,并针对攻击进行防御,彻底弄懂CSRF,话不多说,我们直接开冲。
|
3月前
|
运维 安全 Java
什么是 CSRF?如何防止 CSRF 攻击?
CSRF 攻击是一种常见且危险的 Web 安全漏洞,攻击者可以通过伪造用户请求,执行恶意操作,作为程序员,为了防御 CSRF 攻击,常见的策略包括使用 CSRF Token、检查 Referer 或 Origin 头、设置 SameSite Cookie 属性以及双重提交 Cookie。 因为程序员对于 CSRF 攻击可以做的事情还是很有限,所以,承担主要责任的是安全部门或者运维部门,但是作为程序员,我们需要具备这些安全意识,在安全等级比较高的需求中也需要把这些安全因素考虑在内。