dvcolgan
dvcolgan

Reputation: 7728

Flask giving an internal server error instead of rendering 404

In my Flask app, I set up a 404 handler like this:

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

However, when a user goes to an unrecognized URL, the system gives an internal server error instead of rendering my 404 template. Am I missing something?

Upvotes: 9

Views: 3911

Answers (3)

daaawx
daaawx

Reputation: 3473

There is likely an issue while rendering the 404 template which then triggers an internal server error.

I suggest checking the logs for your app.

Upvotes: 1

Mike Davlantes
Mike Davlantes

Reputation: 1035

This will also occur if you have debug set to true. Try setting debug to false and see if your custom 404 shows up then.

Upvotes: 0

ranendra
ranendra

Reputation: 2512

Internal Server Error is HTTP error 500 rather than 404 and you haven't added error handler for it. This occurs when the server is unable to fulfill the client request properly. To add a gracious message when such error occurred, you can add a errorhandler like 404.

@app.errorhandler(500)
def exception_handler(e):
    return render_template('500.html'), 500

Upvotes: 8

Related Questions