How to return HTTP 400 response in Django? How to return HTTP 400 response in Django? django django

How to return HTTP 400 response in Django?


From my previous comment :

You can return a HttpResponseBadRequest

Also, you can create an Exception subclass like Http404 to have your own Http400 exception.


You can do the following:

from django.core.exceptions import SuspiciousOperationraise SuspiciousOperation("Invalid request; see documentation for correct paramaters")

SuspiciousOperation is mapped to a 400 response around line 207 of https://github.com/django/django/blob/master/django/core/handlers/base.py


If you're using the Django Rest Framework, you have two ways of raising a 400 response in a view:

from rest_framework.exceptions import ValidationError, ParseErrorraise ValidationError# or raise ParseError