Flask Series: Error Management

Logging is used to collect and store valuable information about the application’s execution, that can be used for troubleshooting purposes. Logging includes either storing data to log files, sending it to monitoring services, Windows event logs, Unix syslogs, etc.. Information, being stored and / or sent, can be text messages, call stacks, everything application and environment related, which can be used later on for debugging.

One of the most common approaches used is to log in files. Below you could have a look at the logging configuration and how to use it within a Flask application:

More logging handlers that can be used are:
– RotatingFileHandler;
– NTEventLogHandler;
– SysLogHandler;

You could control the logging data format as well, helpful information how to achieve this goal can be found here.

Error Management
Flask allows to an error handler to a given HTTP status code. There are two types of errors that we will concentrate on in this blog post – application and route errors. Application errors refer to issues in the application code – catching the raised exceptions, etc.. Route errors are about missing or wrong URLs.

Route Errors
Flask provides developers with means to implement their own error handlers via the errorhandler decorator.

Where you need to specify the client or server error codes and to implement a method that returns the content of the page, plus the error code. More information can be found here.

Application Errors
You could either use try/except to handle an exception (where you know it may appear) or provide global error handler.

Example Log

Testing of the Error Handlers 
It is very easy to test the implemented error handlers in the Flask application.

The complete demo application, described in this blog post, can be found here.

In the next blog post I will describe how to secure your Flask application.

Flask Series: Views and Web Forms

In this blog post I will describe how to handle web requests and serve responses for them in a Flask application.

Routing is used to bind a URL to a specific action (function) and allows web applications to have better URLs, easy to remember. Flask provides developers with a route decorator, which is used to register a view function to a given URL rule. More about the route decorator can be found here. It is possible to add variable parts to the URL by using the <var_name> section. Now you could reuse the provided var_name argument in your Flask application by adding a parameter to the view function matching the variable name.

Optionally a converter can be used together with the variable:

– string – default;
– int – accepts integers;
– float – accepts floating point values;
– path – accepts slashes;

There is one more approach that can be used to define routing rules – the add_url_rule() method:

where the first parameter defines the url rule, second – endpoint and third – view function. Further information about the method can be found here.

Tip: Use the add_url_rule method to define the routing rules. When the route decorator is used, all of the code should be imported, that can be a downside to your application. More about the lazily loading views.

Further information about the URL Route Registrations can be found here.

Function-based views
Using this approach you just have to implement a method and decorate it with an endpoint and options, if needed.

Class-based views
Since version 0.7 Flask added support for Pluggable Views (class-based views), where you could implement views as classes, inheritting the View class.

By default Flask handles only GET requests. Should you need to process other kind of a request, you have to provide a class attribute methods:

In case of POST request, you could retrieve the posted values using the request.form.get() method.

It is a tedious job to check the request.method within the dispatch_request method and provide specific processing for GET, POST requests, if any. Here the MethodView approach comes.

More on pluggable views can be found here.

Flashing Messages
Flashing messages provides developers with means to give feedback to the application users. You need to add a secret key, because the session depends on it and the application will throw an error.

Additional information about message flashing in Flask can be found here.

Web Forms
Forms are important part of an application – to submit some data, to login into the system. Validating the forms is also integral part. WTForms provides developers with the needed functionality to achieve these goals. The extension is called Flask-WTF.

Now we have to implement a form class to represent the webform fields:

List with the available fields can be found here.

Validating form fields is easy, you have to pass the validators parameter the corresponding values:

And now you could validate the input using the following approach:

More on validation can be found here.

The complete demo application, described in this blog post, can be found here.

In the next blog post I will describe how to handle errors in your Flask application and log important information for troubleshooting, and debugging purposes.