Learning Flask: Part 4: Errors

Learning Flask

Flask is a microframework for server-side web development in Python.


In Flask, routes normally work with render_template() to react according to HTTP status codes. That is, a normal response is 200 and errors happen in 4XX and 5XX range.

Because 200 is the default, it is not needed for a normal response. However, the most correct response would be to return the code “200” along with any textual data.

Handling Errors

Along with route(), there is also the errorhandler() function. This takes as a parameter the status code or the name of a known exception.

In order to pass back the error, the code should also be returned. This allows the server to correctly register the error to the user.

However, the render_template() can also be used to deliver specific error files or templates along with any other reactions.

Play with the example on Repl.it!