0

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.

3
  • 1
    Instead of saying Server Error (500), you should include the full traceback, from your logs/error email. Commented Jun 15, 2017 at 14:25
  • @Alasdair see edit. Is there another possibly to see the debug page? I don't get an error in the terminal Commented Jun 15, 2017 at 14:30
  • You're right, you can't see the yellow error page, because you need to set DEBUG=False to test the custom 404 page. I would expect to see the traceback in your logs, or the email that is sent to the site admins. Commented Jun 15, 2017 at 14:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.