0

In this, there is:

from flask import render_template

@app.errorhandler(404)
def page_not_found(e):
  return render_template('404.html'), 404

What is e and how do I pass a value to page_not_found to display?

My code is for a REST web service. It import abort, and if a request for a resource results in the resource not available, I call abort(404), as illustrated here (about ¼ down). I would like to provide more information about the nature and circumstances of the 404 to the caller.

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • In this case `e` is the exception instance – wim Feb 11 '16 at 05:14
  • It would help to know what you want to do. But maybe you can use [flashing features](http://flask.pocoo.org/docs/0.10/patterns/flashing/)? You can flash an error and render the error template, and the flashed messages will show. – ATLUS Feb 11 '16 at 05:29
  • When the user gets a 404 page, the browser could have been redirected a few times and thus the address box wouldn't show the original url that eventually resulted in the 404. I would just like to produce some information so that the user can feed back to help trace the cause of the error. – Old Geezer Feb 11 '16 at 07:16

1 Answers1

0

What is e?

It is an Error response object. The documentation isn't clear what to do with it though, from reading the API Spec

How do I pass a value to page_not_found to display

Just use the render_template() method like you would for any other page with values.

@app.errorhandler(404)
def page_not_found(error):
    render_me = "Oh no! Page not found!"
    return render_template('404.html', message=render_me), 404

Where the 404.html could simply be

<html>{{ message }}</html>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I don't think this answers the question fully though. It answers `what is 'e'?` but not passing variables. 404 could be dynamic based on input in the URL, possibly. If you construct your flask URL as ////and_this_ID_is_wrong, then you pass `cascading` and `menu` and `thing` in an @app.route you'd normally do `def method(pass, variables, here):` Could possibly render different error templates based on the variables provided. – ATLUS Feb 11 '16 at 05:23
  • Maybe he wants to show "Event not found, search for another event?" rather than "Page not found," for example. – ATLUS Feb 11 '16 at 05:32
  • @ATLUS - I think I see what you are saying. A generic 404 page with a custom message based on the route. Makes sense now. – OneCricketeer Feb 11 '16 at 05:35
  • Thanks to all. ATLUS is correct about what I wanted to do. @cricket_007: Your updated example still looks static to me. How do I pass a value into the `page_not_found` method? I have imported `abort` and am calling abort(404). – Old Geezer Feb 11 '16 at 07:14
  • @OldGeezer - Oh, you want a parameter to that method? Honestly, I think you would have to write a handler that would throw a 404 redirect to pass in the value to the `error` value, but I'm not well enough versed in Flask to know how to do that, sorry :/ – OneCricketeer Feb 11 '16 at 07:18