• Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)


    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):

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

    干杯!

  • 相关阅读:
    asp.net 对母版页的控件事件
    treeview操作集合
    使用GAppProxy时安全证书无效的解决办法
    向Excel模板中添加数据
    C# 重写 winform 关闭按钮
    完整ASP.Net Excel导入程序(支持2007)
    随笔二则
    标记枚举(flags)的使用
    System.Reflection.Missing.Value与Type.Missing
    Windows下Android源码下载方法
  • 原文地址:https://www.cnblogs.com/hongsandao/p/11389075.html
Copyright © 2020-2023  润新知