class ChromeLoginView(View):

     def get(self, request):
return JsonResponse({'status': request.user.is_authenticated()}) @method_decorator(csrf_exempt)
def post(self, request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return JsonResponse({'status': True})
return JsonResponse({'status': False})

我期待帖子确实由csrf停止,但它返回403错误。

但是如果删除那个装饰器并在URLConf中执行此操作

url(r'^chrome_login/', csrf_exempt(ChromeLoginView.as_view()), name='chrome_login'),

它会工作。

这里发生了什么?它不应该工作,因为我猜这就是method_decorator所做的。我正在使用python3.4和django1.7.1,任何建议都会很棒。

你需要装饰dispatch方法csrf_exempt才能工作。它的作用是csrf_exempt在视图函数本身上设置一个属性True,中间件在(最外层)视图函数上检查它。如果只需要修饰一些方法,你仍然需要csrf_exemptdispatch方法上使用,但你可以使用csrf_protect例如put()。如果GETHEADOPTIONSTRACEHTTP方法用于不管你装饰与否也不会被选中。

class ChromeLoginView(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(ChromeLoginView, self).dispatch(request, *args, **kwargs) def get(self, request):
return JsonResponse({'status': request.user.is_authenticated()}) def post(self, request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return JsonResponse({'status': True})
return JsonResponse({'status': False})

正如@knbk所说,这是dispatch()必须装饰的方法。

从Django 1.9开始,您可以method_decorator直接在类上使用:

from django.utils.decorators import method_decorator

@method_decorator(csrf_exempt, name='dispatch')
class ChromeLoginView(View): def get(self, request):
return JsonResponse({'status': request.user.is_authenticated()}) def post(self, request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return JsonResponse({'status': True})
return JsonResponse({'status': False})

这避免了重写dispatch()方法只是为了装饰它。

如果您正在寻找符合您需求的Mixins,那么您可以创建一个CSRFExemptMixin并在您的视图中扩展它,无需在每个视图中编写上述语句:

class CSRFExemptMixin(object):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(CSRFExemptMixin, self).dispatch(*args, **kwargs)

之后在你的视图中扩展这个。

class ChromeLoginView(CSRFExemptMixin, View):

您可以根据您的要求在任何视图中扩展它,这是可重用性!:-)

干杯!

Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)的更多相关文章

  1. Django:之Sitemap站点地图、通用视图和上下文渲染器

    Django中自带了sitemap框架,用来生成xml文件 Django sitemap演示: sitemap很重要,可以用来通知搜索引擎页面的地址,页面的重要性,帮助站点得到比较好的收录. 开启si ...

  2. Django 1.10中文文档-第一个应用Part4-表单和通用视图

    本教程接Part3开始.继续网页投票应用程序,并将重点介绍简单的表单处理和精简代码. 一个简单表单 更新一下在上一个教程中编写的投票详细页面的模板polls/detail.html,让它包含一个HTM ...

  3. Django通用视图执行过程

    使用通用视图后,Django请求处理过程(以ListView为例):在我们自定义的视图中: class IndexView(ListView): template_name = 'blog/index ...

  4. Django 基于类的通用视图

    在早期,我们认识到在视图开发过程中有共同的用法和模式.这时我们引入基于函数的通用视图来抽象这些模式以简化常见情形的视图开发. 基于函数视图的用法有以下三种: def index(request): r ...

  5. Django 1.6 基于类的通用视图

    Django 1.6 基于类的通用视图 最初 django 的视图都是用函数实现的,后来开发出一些通用视图函数,以取代某些常见的重复性代码.通用视图就像是一些封装好的处理器,使用它们的时候只须要给出特 ...

  6. Django的rest_framework的视图之基于通用类编写视图源码解析

    我们上一篇博客讲解了如何使用mixins类实现rest_framework的视图,但是其中有很多的冗余的代码,我们这边在来优化一下 1.queryset的视图函数 首先看下对queryset操作的视图 ...

  7. Django——django1.6 基于类的通用视图

    最初 django 的视图都是用函数实现的,后来开发出一些通用视图函数,以取代某些常见的重复性代码.通用视图就像是一些封装好的处理器,使用它们的时候只须要给出特定的参数集即可,不必关心具体的实现.各种 ...

  8. Python Django CBV下的通用视图函数

    ListView TemplateView DetailView 之前的代码实例基本上都是基于FBV的模式来撰写的,好处么,当然就是简单粗暴..正如: def index(request): retu ...

  9. Django的rest_framework的视图之基于ModelViewSet视图源码解析

    前言 今天一直在整理Django的rest_framework的序列化组件,前面一共写了2篇博客,前面的博客给的方案都是一个中间的状态的博客,其中有很多的冗余的代码,如果有朋友不清楚,可以先看下我前面 ...

随机推荐

  1. AOP 相关包

    aop 命名空间,jar 扫路径

  2. 使用WireMock伪造REST服务

    在真正的rest api服务还没有写好之前,为了方便前端测试调用,后端可以写个服务,伪造rest服务(写假数据) 1.官网: http://wiremock.org/ 下载可执行jar:http:// ...

  3. wannafly 挑战赛8 E 小G的项链(manecher)

    链接:https://www.nowcoder.com/acm/contest/57/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...

  4. wannalfy 挑战赛7 E 珂朵莉与GCD (离线+线段树/树状数组)

    链接:https://www.nowcoder.com/acm/contest/56/E 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 716800K,其他语言1433600K 6 ...

  5. 大数据笔记(二十五)——Scala函数式编程

    ===================== Scala函数式编程 ======================== 一.Scala中的函数 (*) 函数是Scala中的头等公民,就和数字一样,可以在变 ...

  6. Chrome 66 新增异步剪贴板 API

    在过去的几年里我们只能使用 document.execCommand 来操作剪贴板.不过,这种操作剪贴板的操作是同步的,并且只能读取和写入 DOM. 现在 Chrome 66 已经支持了新的 Asyn ...

  7. CentOS7服务器配置

    CentOS7服务器配置 1.更换yum软件源 下载阿里源 cd /etc/yum.repos.d sudo wget -nc http://mirrors.aliyun.com/repo/Cento ...

  8. C/C++判断字符串是否包含某个字符串

    C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...

  9. 使用virt-manager创建无盘工作站(基于nfs文件系统)

    首先需要做些准备工作: 1.安装xming及virt-manager, 安装过程网上很多,就不一一叙述了.安装完成后,使用putty登陆服务器,需要配置如下: connection -> SSH ...

  10. python 中文路径

    ipath = 'D:/学习/语料库/SogouC.mini/Sample/C000007/10.txt' uipath = unicode(ipath , "utf8")