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

django jsonresponse(('status code))
http status codes
django response
return httpresponse django
http 204
render response django
http status code must be an integer.
django response vs httpresponse

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?

return HttpResponse(status=204)

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  The HTTP 204 No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page. A 204 response is cacheable by default. An ETag header is included in such a response.

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()

[SOLVED] How can I return HTTP status code 204 from a Django , [SOLVED] How can I return HTTP status code 204 from a Django view? | Python Language Knowledge Base. REST framework includes a set of named constants that you can use to make your code more obvious and readable. from rest_framework import status from rest_framework.response import Response def empty_view(self): content = {'please move along': 'nothing to see here'} return Response(content, status=status.HTTP_404_NOT_FOUND)

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.)

Status codes, from rest_framework import status from rest_framework.response import Response def empty_view(self): content = {'please move along': 'nothing to see here'}  from django.http import HttpResponse def my_view (request): # # Return a "created" (201) response code. return HttpResponse ( status = 201 ) Because 404 errors are by far the most common HTTP error, there’s an easier way to handle those errors.

If you are returning render(), according to the Django documentation, render() takes status as an optional parameter, and its default value is 200.

Therefore, if you want to return a custom status use:

def my_view(request):
    return render(request,
                  'your-template.html',
                   context=your_context,
                   status=204) # your custom status in this case 204

HTTP Status 204 (No Content) – REST API Tutorial, HTTP Status 204 (No Content) indicates that the server has successfully fulfilled the request which if present SHOULD be applied to current document's active view if any. The 204 response MUST NOT include a message-body and thus is always HTTP Methods · Richardson Maturity Model · HTTP Response Codes. JsonResponse normally returns HTTP 200, which is the status code for 'OK'. In order to indicate an error, you can add an HTTP status code to JsonResponse as it is a subclass of HttpResponse : response = JsonResponse({'status':'false','message':message}, status=500)

204 No Content, HTTP Status Code 204: The server has successfully fulfilled the request and For example, if a 204 status code is received in response to a PUT request user agent does not need to traverse away from its current "document view" (if any). REST framework provides more explicit identifiers for each status code, such as HTTP_400_BAD_REQUEST in the status module. It's a good idea to use these throughout rather than using numeric identifiers. Wrapping API views. REST framework provides two wrappers you can use to write API views. The @api_view decorator for working with function based views.

(204 (No Content) responses without content-type crashes django if , A HTTP 204 response (no content) doesn't return an entity-body in the request, but Django is forced to include a content-type header because of . 204 No Content is not terribly useful as a response code for a browser (although according to the HTTP spec browsers do need to understand it as a 'don't change the view' response code). 204 No Content is however, very useful for ajax web services which may want to indicate success without having to return something.

Writing views | Django documentation, The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as  2.http - Django "The view didn't return an HttpResponse object." 3. python - How can I return HTTP status code 204 from a Django view? 4. unit testing - How to simulate a HTTP Post request from a django view without a template

Comments
  • I haven't done this before, but have you tried setting the status attribute of the response object before returning the response from your view? Also, here is another SO question about this: stackoverflow.com/questions/408541/…
  • Thanks. W3C states "The 204 response MUST NOT include a message-body" so (assuming the content parameter maps to the message body) it should be blank though, right?
  • For anyone reading who isn't just concerned with a 204 code you can use a message body with Django's HTTPResponse methods.
  • Wouldn't this cause a redirect though?
  • No redirect needed, it should do the same as the top answer, just shorter syntax. (Using status code 204 for a redirect would be very weird, if it works at all).
  • Oh.. Is there a way I could pass an error status without actually reloading the page or redirecting to new page? I want to use it when a file that user is trying to save already exists. I tried 409 & 422. But, they redirect it to new page. I know AJAX is the other solution. But was wondering if Django had any trick.
  • Status codes are used when visitors are loading a page. So they have to be already loading a page; changing the code doesn't cause an extra redirect but you're still loading a new page. If they're on a page and you want to inform them without reloading, then I don't think there's a way to update the code of the current page (I don't think it would be terribly useful anyway). So: if they save a form which sends them to a new page, then you can choose any status code for the result page. If it doesn't load a new page, just show message and forget status code.
  • I get what you're saying. But, I really need to do just update the page. So, I guess, I'll have to use AJAX + Django :( Thanks a lot though!