Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 659
davidscolgan Avatar asked Feb 09 '12 14:02

davidscolgan


People also ask

How do I fix 404 error in Flask?

To handle 404 Error or invalid route error in Flask is to define a error handler for handling the 404 error. @app. errorhandler(404) def invalid_route(e): return "Invalid route." Now if you save the changes and try to access a non existing route, it will return “Invalid route” message.

What is Internal server error in Flask?

Either the server is overloaded or there is an error in the application. This is the 500 Internal Server Error, which is a server error response that indicates that the server encountered an internal error in the application code. The traceback above goes through the code that triggered the internal server error.

How do I fix an internal server error in Python?

With Perl or Python scripts, a common reason for an “Internal Server Error” message is that you've uploaded a script in the wrong "mode" in your FTP program. In particular, Perl and Python scripts should always be uploaded in "ASCII" or "text" mode, not "binary" mode.


2 Answers

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
like image 134
ranendra Avatar answered Oct 16 '22 12:10

ranendra


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.

like image 39
daaawx Avatar answered Oct 16 '22 14:10

daaawx