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()
.

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.
Infos
- Django: https://www.djangoproject.com
- Flask: http://flask.pocoo.org
- Geistesblitze: https://github.com/nnrcschmdt/geistesblitze
- Denied micro web framework: http://denied.immersedcode.org
- Flask history: http://lucumr.pocoo.org/2010/4/3/april-1st-post-mortem/
- The Werkzeug toolkit: http://werkzeug.pocoo.org/docs/
- Jinja 2: http://jinja.pocoo.org/docs/
- The developer team: http://www.pocoo.org/team/
- Flask Bootstrap: https://pythonhosted.org/Flask-Bootstrap/
- Flask Login: https://pythonhosted.org/Flask-Login/
- Flask SQL Alchemy: https://pythonhosted.org/Flask-SQLAlchemy/
- Flask WTF: https://flask-wtf.readthedocs.org/en/latest/
- WT Forms: https://wtforms.readthedocs.org/en/latest/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.