Search code examples
djangoerror-handlinghttp-status-code-500

Custom 500 error getting blank page sometimes


When I try to test the custom 500 error page. If the test works, the I get a blank page with nothing on it ("Internal Server Error") in my error logs. If an error occurs, I get custom error page.

In views:

def handler500(request):
    response = render_to_response("500.html")
    response.status_code = 500
    return response

def my_test_500_view(request):
    # Return an "Internal Server Error" 500 response code.
    return HttpResponse(status=500)

In Urls.py:

my path is : path('test500/', views.my_test_500_view, name='test500')

Also:

handler500 = 'myapp.views.handler500'

If I actually create an error like by removing the import line for the HttpResponse from the my_test_500_view above, the custom 500 error page actually loads and I get this error on the server, i.e. the actual error.

NameError: name 'HttpResponse' is not defined

What's wrong with my test that it returns a blank page. Is my 500 page actually working? I think it's not working right, since I got a email warning from google search about failure of 500 error page and I often get this error:

TypeError: view must be a callable or a list/tuple in the case of include().

Please help, what's going on.


Solution

  • Returning an HttpResponse(status=500) does not cause Django to call the handler500 view, it just returns a blank page (you didn't specify any content in HttpResponse) and sets status 500 on the response headers.

    If you want to test your handler500, just raise an exception in your test view. Any exception will do.