How to disable Python Django’s CSRF validation?

Sometimes, we want to disable Python Django’s CSRF validation.

In this article, we’ll look at how to disable Python Django’s CSRF validation.

How to disable Python Django’s CSRF validation?

To disable Python Django’s CSRF validation, we can use the csrf_exempt decorator on a view.

For instance, we write

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')

to create the my_view view.

We use the csrf_exempt decorator to make Django skip CSRF validation when making a request to this view.

Conclusion

To disable Python Django’s CSRF validation, we can use the csrf_exempt decorator on a view.