I'm using flask to manage my website, I'm trying to includes a simple 404 page error, but I keep getting the plain one from flask. Here is my code:
flask app
@views.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'), 404
html
{% with title="404 Not Found" %}
{% include "head.html" %}
{% endwith %}
<body>
<h1>404 Page not found</h1>
</body>
</html> ### I need to include the </html> tag because it's in the head.
However I'm getting this:
CodePudding user response:
Since you have registered the error handler with views
, I presume you are working with blueprints. If this assumption is correct, please note the last paragraph in the "Handling" section:
Handlers registered on the blueprint take precedence over those registered globally on the application, assuming a blueprint is handling the request that raises the exception. However, the blueprint cannot handle 404 routing errors because the 404 occurs at the routing level before the blueprint can be determined.
To achieve the desired behaviour, register this error handler with the "app" returned by the Flask(__name__)
initialization.
CodePudding user response:
Is it just me, or should you change
@views.errorhandler(404)
to
@app.errorhandler(404)
Have a look at the original docs: https://flask.palletsprojects.com/en/2.1.x/errorhandling/