2019独角兽企业重金招聘Python工程师标准>>>

该解决办法是根据个技术博文上修改后的,因以为原方法无法解决这个错误,以下内容是根据我实际操作过程修正的。

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.

解决办法:

在django1.5里,就必须按照提示的步骤来:

1,在view里面,强制使用RequestContext 代替Context。示例如下:

from django.template import RequestContext

视图返回要这样:

c = RequestContext(request,{

'todos': todos,

'choices': importance_choices, })

return HttpResponse(t.render(c)) )

2,在View里,添加@csrf_protect示例如下:

from django.views.decorators.csrf import csrf_protect

@csrf_protect

def add_todo(request):

t = Todo(

title = request.POST['title'],

description = request.POST['description'],

importance = request.POST['importance'], )

t.save()

return HttpResponseRedirect(reverse(todo_index))

2,在模板的form体里面,加入 {% csrf_token %},示例如下:

  {% csrf_token %}

……

4、检查一下中间件里'django.middleware.csrf.CsrfViewMiddleware',是否正常开放。默认是直接开着的。

转载于:https://my.oschina.net/zhangzhe/blog/414006

Django错误解决: CSRF verification failed. Request abor相关推荐

  1. Django POST CSRF verification failed. Request abor

    问题 在使用Django搭建好测试环境后,写了一个提交POST表单提交留言的测试页面. 如图: 填写表单,点击"提交留言"按钮提交到服务器,却出现 Forbidden (403) ...

  2. Django web : CSRF verification failed. Request aborted.

    错误标题:CSRF verification failed. Request aborted. 错误描述: HelpReason given for failure:CSRF cookie not s ...

  3. Django 3.1.5, CSRF verification failed. Request aborted.

    post请求django的时候出现:CSRF verification failed. Request aborted 问题翻译: 一般而言,这可以发生时,有一个真正的跨站请求伪造,或当Django的 ...

  4. CSRF verification failed. Request aborted.的解决办法

    django1.21加入了防止CSRF攻击的模块. 这是django的debug模式下给出的错误提示页面. Forbidden (403) CSRF verification failed. Requ ...

  5. Python Django提交表单时报错:Forbidden (403) CSRF verification failed. Request aborted.

    报错截图: 解决方法: 把settings里面一个叫csrf的安全机制的注释掉即可. MIDDLEWARE = ['django.middleware.security.SecurityMiddlew ...

  6. CSRF verification failed. Request aborted.

    当运行一个django项目后,出现了这样的一个错误,如图: 这时我们只要在settings.py中注释掉一行即可. 'django.middleware.csrf.CsrfViewMiddleware ...

  7. CSRF verification failed. Request aborted. 表单提交方法为POST时的报错

    本人所用Django版本为1.11,在设置请求方法为POST时,遇到标题中的错误,尝试了多种方法,最终通过下面的操作来修复: 在template文件中添加图中红框部分 接着,导入csrf_exempt ...

  8. 【ubuntu】解决 Certificate verification failed: The certificate is NOT trusted

    目录 1.遇到问题 2.问题解决 1.遇到问题 Certificate verification failed: The certificate is NOT trusted. The certifi ...

  9. django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方案

    现象 表单界面如下: 在点击提交之后,出现如下错误页面: HTML的代码如下: contact_form.html <!DOCTYPE HTML PUBLIC ><html> ...

最新文章

  1. Bitcoin 中的挖矿算法(2) 难度值说明
  2. Docker Compose——搭建Redis集群
  3. 信息论-Shannon entropy-Kullback-Leibler (KL) divergence-cross-entropy
  4. 以下哪个选项不是单例模式的优点_深度解密Python单例模式
  5. C语言中数组做函数参数的问题
  6. windows系统改装为linux系统_Linux怎么克隆系统?备份系统跟Windows系统有区别吗?...
  7. 如何显示python的内置模块_python之模块(内置模块)
  8. Windows Embedded Webcast 2008年1月预告
  9. sql 如何设置行级锁_SQL Server中的行级安全性简介
  10. python读取bmp文件,go 读取BMP文件头二进制读取方式
  11. 微信H5支付功能开发
  12. 周立功上位机获取CAN通讯数据解析 V2.0
  13. 00.Spring源码编译
  14. SAP培训学校的选择
  15. Jenkins忘记登陆账号和密码的解决办法
  16. CTF_RSA_N不互素
  17. Python 之 pygame 学习(入门知识)
  18. RTK模块性能测试分析对比-GNSS实测- RTK板卡100赫兹延迟实测及分析
  19. win10的开机启动文件夹
  20. Typora+github代替云笔记(git上传文件至github)

热门文章

  1. 实例解读:如何减少Docker中的Java内存消耗
  2. Redis高级特性介绍及实例分析
  3. 菜鸟程序员如何提升自己的潜在价值
  4. AMD5470显卡Ubuntu下的U盘的使用
  5. LeetCode 1497. 检查数组对是否可以被 k 整除
  6. 任务分发系统-Qcmd-http详解
  7. File类的用法总结,及文件过滤器的介绍。
  8. linux用户退出时自动清除last记录,Linux查看用户登陆历史记录(last命令的使用)
  9. netty 常见疑问 faq
  10. 在Spring Boot 项目中使用Spring AOP实现切面日志