I used the following code to generate a dynamic error message page, which includes 404. It passes along the error code, the name of the error, and a description of the error.
from werkzeug.exceptions import HTTPException, InternalServerError
# This generates a dynamic HTTP exception page
@app.server.errorhandler(HTTPException)
def handle_http_exception(e):
return render_template("http_exception.html", code=e.code, name=e.name, description=e.description)
# This responds with an internal server error page
@app.server.errorhandler(InternalServerError)
def handle_internal_server_error(e):
return render_template("http_exception.html", code=e.code, name=e.name, description=e.description)
Then, I created a ‘templates’ folder in the same directory as my python application (.py). Within that folder is a file called ‘http_exception.html’ with the following contents:
<html>
<head>
<title>{{ code }} - {{ name }}</title>
</head>
<body>
<br /><br /><br /><br />
<table width='100%'>
<tr>
<td align='center'>
<p><font size="5" color="red">{{ code }} - {{ name }}</<br /><br /></font><br />
<b>{{ description }}</b>
</p>
</table>
</body>
</html>
Here’s an example screenshot:
