How can I return HTTP status code 204 from a Django view?

I want to return status code 204 No Content from a Django view. It is in response to an automatic POST which updates a database and I just need to indicate the update was successful (without redirecting the client).

There are subclasses of HttpResponse to handle most other codes but not 204.

What is the simplest way to do this?


Solution 1:

return HttpResponse(status=204)

Solution 2:

When using render, there is a status keyword argument.

return render(request, 'template.html', status=204)

(Note that in the case of status 204 there shouldn't be a response body, but this method is useful for other status codes.)

Solution 3:

Either what Steve Mayne answered, or build your own by subclassing HttpResponse:

from django.http import HttpResponse

class HttpResponseNoContent(HttpResponse):
    status_code = 204

def my_view(request):
    return HttpResponseNoContent()