Exploring the Flask web framework in Python

GET It, POST It

The views follow. A GET request puts the login form on the screen; a POST request attempts to validate the form (Listing 6). If the password is correct, application logs the user on (flask.ext.login.login_user()) and forwards the user to their own idea list. Otherwise, the login() function displays an error.

Listing 6

Login Form

 

The logout() function does something similar; it displays a message to the user when the user logs off and returns to the login page. A new user who registers gets to see the user registration form (thanks to the register() function) following a GET request, while the application validates the form after a POST request. The idea(id) function reveals the details of the flashes of genius. If the idea is missing or did not come from the logged-on user, an appropriate error page appears (Listing 7).

Listing 7

Ideas in Detail

 

In contrast to idea(id), the function ideas() shows all the ideas belonging to the currently logged-on user (Figure 3). If the user wants to add a new idea, a GET request shows the appropriate form, a POST request validates whether a name and a description exist for the idea. If this is the case, add_idea() (Listing 8) adds the idea, saves the results, outputs a message, and redirects the user to the list of ideas().

Figure 3: The idea collection lists all the ideas belonging to the logged-on user.

Listing 8

Adding New Ideas

 

On Screen!

As a central component of Flask, the Jinja 2 template engine helps to put the content on screen. Inspired by the templates from Django, Jinja 2 offers several features that make it a flexible and fast system. The developer can inherit from templates, override blocks, and also extend blocks. When combined with the Bootstrap extension, the result is a very flexible framework for writing applications.

Whether the base template (Listing  9) in the navigation bar displays the link to the list of ideas or the links for logging and registering depends on whether or not the user logs in. If the system still has messages for the user, they appear in the contents pane.

Listing 9

Basic Template

 

The Bootstrap Flask extension also contains macros that make it very easy to display forms. To display the form for registering, logging in, or adding a new idea, call the wtf.quick_form(form) function (Listing 10).

Listing 10

Flask Bootstrap

 

Flask 1.0

The current 0.10.1 version of the Flask micro framework is now more than a year old, which you can take as a sign of stability and maturity. The bugfix version 0.10.2 and the 1.0 version have already been on the roadmap for some time. Even if no date is set for the release, it will probably not be long until we see version 1.0 of Flask. In this case, the April Fool's joke could suddenly become serious.

The Author

Ernesto Rico Schmidt was born and grew up in Bolivia. He studied electrical engineering at the Technical University of Graz in Austria. He is a developer, Python enthusiast, and long-time Linux user.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Nix and NixOS

    NixOS and the Nix package manager offer a promising new approach to the challenge of managing packages in Linux.

  • Password Tools

    Create secure passwords with the help of a password generator and check for quality at the same time.

  • Pass Password Manager

    Pass is a simple shell script that helps you manage and synchronize passwords using Git.

  • Open-Xchange and Zarafa

    The Open-Xchange and Zarafa groupware systems can tap into the APIs of Facebook, Twitter, and Xing, but you need different tactics for each service – keep in mind that the information yield is sometimes quite meager.

  • Automated Web Logins

    Automated web logins with command-line tools and Selenium ensure you don't miss scheduling an activity.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News