Using django 1.11, I have defined a custom 404 handler, by writing in my main URLs.py:
handler404 = 'myApp.views.myError'
The function myError()
looks like this:
def myError(request):
#errorMsg=request.error_404_message
return render(request,'404.html')
This function works when I call raise Http404("My msg")
.
However, when I try to get the message passed by the Http404
call (see commented section in myError), my code crashes:
Server Error (500)
How can I then get the error message? I read a lot of questions about it like this one, but most answers refer to getting the message in the html file and not in a custom python error handler function.
EDIT: The problem is, that I cannot see the debug page, because the error obviously only occurs when myError() is called and this happens only, if debug=False
in the settings.
The request does not have a error_404_message
attribute by default, so request.error_404_message
will give you an error.
The answer you linked to sets it as an attribute, but I do not see why you would want to do this in Django 1.9+, since you already have access to the exception in the view.
The signature of your custom view should be:
def myError(request, exception, template_name='404.html'):
...
You now have access to exception
. You can look at the default page not found view to how it extracts the message from the exception.