Django @csrf_exempt not working in class View

前端 未结 3 631
半阙折子戏
半阙折子戏 2021-01-11 21:43

I have an application in Django 1.9 that uses SessionMiddleware. I would like to create an API for this application inside the same project, but when doing a POST request it

3条回答
  •  既然无缘
    2021-01-11 22:12

    You need to decorate the csrf_exempt inside the dispatch method.

    class MyView(FormView):
    
        @method_decorator(csrf_exempt)
        def dispatch(self, *args, **kwargs):
            return super(MyView, self).dispatch(*args, **kwargs)
    
        def post(self, request, *args, **kwargs):
            # ....
            return super(MyView, self).post(request, *args, **kwargs)
    

提交回复
热议问题