玩转Django的POST请求 CSRF

不少麻油们玩django都会碰到这个问题,POST请求莫名其妙的返回 403 foribidden,希望这篇博文能解答所有问题

三种方法

  1. To enable CSRF protection for your views, follow these steps:
  1. 1. Add the middleware`django.middleware.csrf.CsrfViewMiddleware` to your list ofmiddleware classes in `setting.py`, MIDDLEWARE_CLASSES. (It should comebefore any view middleware that assume that CSRF attacks havebeen dealt with.)
  2. Alternatively, you can use the decorator `@csrf_protect` on particular viewsyou want to protect (see below).

我尝试了@csrf_exempt也可以呢

8@csrf_exempt的作用是对当前view方法关闭CSRF

  1. 2. In any template that uses a POST form, use the csrf_token tag insidethe <form> element if the form is for an internal URL, e.g.:
  2. `<form action="." method="post">{% csrf_token %}`
  3. This should not be done for POST forms that target external URLs, sincethat would cause the CSRF token to be leaked, leading to a vulnerability.
  1. 3. In the corresponding view functions, ensure that the`django.core.context_processors.csrf` context processor isbeing used. Usually, this can be done in one of two ways:
  2. Use RequestContext, which always uses`django.core.context_processors.csrf` (no matter what yourTEMPLATE_CONTEXT_PROCESSORS setting). If you are usinggeneric views or contrib apps, you are covered already, since theseapps use RequestContext throughout.
  3. Manually import and use the processor to generate the CSRF token andadd it to the template context. e.g.:
  4. from django.core.context_processors import csrf
  5. from django.shortcuts import render_to_response
  6. def my_view(request):
  7. c = {}
  8. c.update(csrf(request))
  9. # ... view code here
  10. return render_to_response("a_template.html", c)
  11. You may want to write your ownrender_to_response() wrapper that takes careof this step for you.
  12. The utility script extras/csrf_migration_helper.py can help to automate thefinding of code and templates that may need these steps. It contains full helpon how to use it.

说白了就是需要这些东东

提交的时候得有个csrfmiddlewaretoken

  1. <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

ajax提交的时候就需要手动添加了:

django在加载form的时候会生成token,同时加到了cookie中

  1. var param = $.param($('#addipModal :input:not(button)'));
  2. $.ajax({
  3. url: "{% url 'attendence:ip_add'%}",
  4. method: "post",
  5. data: param + "&csrfmiddlewaretoken=" + $.cookie('csrftoken'),
  6. success: function(data) {
  7. $("#cancelip").click();
  8. alert(data);
  9. window.location.reload();
  10. }
  11. });

附官方文档地址:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

玩转Django的POST请求 CSRF的更多相关文章

  1. Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法

    Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...

  2. Django学习系列之CSRF

    Django CSRF 什么是CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到 ...

  3. token、cookie和session区别以及django中的cookie,csrf

    参考:https://my.oschina.net/xianggao/blog/395675?fromerr=GC9KVenE [前言]登录时需要post的表单信息. 先跳过具体案例,讲解基础知识: ...

  4. Django跨域请求之JSONP和CORS

    现在来新建一个Django项目server01,url配置为 url(r'^getData.html$',views.get_data) 其对应的视图函数为get_data: from django. ...

  5. Django的安全机制 CSRF 跨站请求访问

    跨站请求伪造 一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防 ...

  6. Django后台post请求中的csrf token

    使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在c ...

  7. Django中间件如何处理请求

    Django中间件 在http请求 到达视图函数之前   和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. Django1.9版本以后中间件的执行流程 1. ...

  8. 10 Django之Ajax请求

    一.什么是Ajax技术? 异步的JavaScript和XML.使用Javascript语言与服务器进行异步交互,传输的数据为XML(更多的使用json数据).Ajax不是一门新的编程语言,而是一种使用 ...

  9. python——django的post请求

    两次被同一块石头绊倒简直不可原谅!第一次写django程序的时候,就因为ajax post请求折腾了整整一天,时隔两个多月昨天又被虐一整晚.叔可忍婶儿也不能忍了!!!重要的事情写下来,为以后轻松碾压p ...

随机推荐

  1. Git很好的教程

    本文地址:http://www.cnblogs.com/yhLinux/p/4067064.html 很好的Git教程,作为初学者,跟着作者的教程走了一遍之后,基本熟悉了Git的常用操作,此教程简洁明 ...

  2. 命令安装VS

     Installing Visual Studio Visual Studio 2015   Other Versions Visual Studio 2013 Visual Studio 2010 ...

  3. out与ref的区别

    out与ref的区别  前者传参时不必初始化,后者需要初始化 int a,b; public void fName(out int a,out int b); fName(out a , out b) ...

  4. Windows XP和Word 2007不能正常使用VSTO插件

    今天帮助同事解决了一个小问题,就是在WindowsXP上,为Word2007开发的插件不能正常显示. 通过搜索关键词 WindowsXp Word 2007 VSTO找到了两个解决方案. http:/ ...

  5. java-API中的常用类,新特性之-泛型,高级For循环,可变参数

    API中的常用类 System类System类包含一些有用的类字段和方法.它不能被实例化.属性和方法都是静态的. out,标准输出,默认打印在控制台上.通过和PrintStream打印流中的方法组合构 ...

  6. WINDOWS窗口风格 WS_OVERLAPPEDWINDOW

    转自:http://blog.csdn.net/hquxiezk/archive/2008/07/29/2733269.aspx #define WS_OVERLAPPEDWINDOW (WS_OVE ...

  7. ieee80211w

    80211w概述 1, WLAN网络在设计的时候就容易遭受各种类型的Denial of Service(DOS)攻击, a, 射频干扰(RF jamming) b, Spoofed Disconnec ...

  8. svn patch用法

    最近遇到了一个patch的使用场景: 有一个同事对源码做了一些修改,但是又不想将源码提交到SVN服务器,而我又想得到他所做的修改. patch的使用方法: 创建patch 在要导出“修改”的目录中,单 ...

  9. UML-用例图

    用例图是指由参与者.用例以及它们之间的关系构成的用于描述系统功能的视图.用例图是被称为参与者的外部用户所能观察到的系统功能的模型图,呈现了一些参与者和一些用例,以及它们之间的关系,主要用于对系统.子系 ...

  10. Web Essentials之Markdown和自定义编辑器(Web Essentials完结)

    返回Web Essentials功能目录 本篇目录 功能 自定义编辑器 开源项目都会在项目的根目录放一个Readme.md文件来告诉读者一些重要的说明,那么就可以在VS中直接编辑Markdown文件. ...