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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.