Django HTTP 500 Error
The HttpResponseServerError
inherits from HttpResponse
and is actually quite simple:
class HttpResponseServerError(HttpResponse):
status_code = 500
So let's look at the HttpResponse
constructor:
def __init__(self, content='', *args, **kwargs):
super(HttpResponse, self).__init__(*args, **kwargs)
# Content is a bytestring. See the `content` property methods.
self.content = content
As you can see by default content
is empty.
Now, let's take a look at how it is called by Django itself (an excerpt from django.views.defaults):
def server_error(request, template_name='500.html'):
"""
500 error handler.
Templates: :template:`500.html`
Context: None
"""
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
return http.HttpResponseServerError('<h1>Server Error (500)</h1>')
return http.HttpResponseServerError(template.render(Context({})))
As you can see when you produce a server error, the template named 500.html is used, but when you simply return HttpResponseServerError
the content is empty and the browser falls back to it's default page.
Put this below in the urls.py.
#handle the errors
from django.utils.functional import curry
from django.views.defaults import *
handler500 = curry(server_error, template_name='500.html')
Put 500.html in your templates. Just as simple like that.