[Fixed]-How does HttpResponse(status=<code>) work in django?

13đź‘Ť

âś…

HttpResponse(status=[code]) is just a way of sending the actual HTTP status code in the header of the response. It arrives at the client with that status_code, but does not do any changing of the data except for the HTTP header. You can pass any status code with a properly-working response, and it will still show up as before, but if you go into the browser’s console you will see that it read it as a “405” page.

HTTP headers are simply transferred with each request and response for web server parsing and providing metadata/info to developers. Even 404 pages have content sent with them, to tell the user that there was a 404; if it didn’t, the user would just get a blank page and have no idea what went wrong.

If you want to signify an error, you can take a look at these docs. Alternatively, you can use the HttpResponseRedirect (see here) option to direct to an error-view that serves a custom error response.

👤Matthew R.

2đź‘Ť

Django lets you specify error handlers for handler400, handler403, handler404 and handler500 in your urls.py. It does not support a handler405.

Note that you can return http responses with any status, and Django will return that response to the user, it won’t call the handler for that status code.

The error handlers are called when an exception is raised. For example, if Http404 is raised in a view, Django will call the handler404 view.

👤Alasdair

Leave a comment