@csrf_exempt does not work on generic view based class

后端 未结 4 2110
傲寒
傲寒 2020-11-29 01:14
class ChromeLoginView(View):

     def get(self, request):
          return JsonResponse({\'status\': request.user.is_authenticated()})

     @method_decorator(csrf_         


        
4条回答
  •  清歌不尽
    2020-11-29 02:08

    If you are looking for Mixins to match your needs, then you can create a CSRFExemptMixin and extend that in your view no need of writing above statements in every view:

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

    After that Extend this in your view like this.

    class ChromeLoginView(CSRFExemptMixin, View):
    

    You can extend that in any view according to your requirement, That's reusability! :-)

    Cheers!

提交回复
热议问题