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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.