csrf_exempt for class based views

class ErrorReportView(View):

    def get(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')

    @method_decorator(csrf_exempt)
    def post(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')

enter image description here

I use Postman, but I get

<p>CSRF verification failed. Request aborted.</p>

Documentation: https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/#decorating-the-class

Could you help me?

>Solution :

The csrf_exempt decorator should be applied to "dispatch" method. You can add the following decorator directly on top of your class, like so:

@method_decorator(csrf_exempt, name='dispatch')
class ErrorReportView(View):

    def get(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')

    def post(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')

Leave a Reply