Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading