English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A good GUI application needs to provide interactive feedback information to the user. For example, desktop applications use dialog boxes or message boxes, and JavaScript uses the alert() function for similar purposes.
It is easy to generate such information messages in Flask web applications. The flashing system of the Flask framework allows you to create a message in one view and display it in the next view function named 'next'.
The Flask module contains the flash() method. It passes the message to the next request, which is usually a template.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 flash(message, category)
Here -
message - The parameter is the actual message to be refreshed. category - The parameter is optional. It can be 'error', 'info', or 'warning'.
To remove messages from the session, the template calls the get_flashed_messages() function.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 get_flashed_messages(with_categories, category_filter)
Both parameters are optional. If the received message has a category, the first parameter is a tuple. The second parameter is very useful for displaying specific messages.
The following flashing template receives the message.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %} {% endwith %}
/,
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 @app.route('/') def index(): return render_template('index.html')
This link guides the user to the URL that displays the login form => “/login. When submitted, the login() function verifies the username and password, and flashes the corresponding 'Success' or 'Error' variable message.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 @app.route('/login', methods=['GET', 'POST']) def login(): error = None if request.method == 'POST': if request.form['username'] != 'admin' or \ request.form['password'] != 'admin': error = 'Invalid username or password. Please try again!' else: flash('You were successfully logged in') return redirect(url_for('index')) return render_template('login.html', error=error)
If there is an error, the login template will be displayed again and the error message will be shown.
Template file: login.html The code is as follows -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Flask Example</title> </head> <body> <h1>Login</h1> {% if error %} <p><strong>Error:</strong> {{ error }} {% endif %} <form action="}}/login" method="POST"> dl> <dt>Username:</dt>/dt> dd> <input type="text" name="username" value="{{ request.form.username }}"> </dd> <dt>Password:</dt>/dt> <dd><input type="password" name="password"></dd> </dl> <p><input type="submit" value="Login"></p> </form> </body> </html>
If login is successful, a success message is flashed on the index template. The following code is saved in the file( index.html) -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Flask Message Flashing</title>/title> </head> <body> {% with messages = get_flashed_messages() %} {% if messages %} <ul class="flashes"> {% for message in messages %} <li>{{ message }}</li>/li> {% endfor %} </ul> {% endif %} {% endwith %} <h1>Flask Message Flashing Example</h1> <p>You want to <a href="{{ url_for('login') }}"> <b>Login?</b>/b></a></p> </body> </html>
The complete code for the Flask message flashing example is shown below -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 from flask import Flask, flash, redirect, render_template, request, url_for app = Flask(__name__) app.secret_key = 'random string' @app.route('/') def index(): return render_template('index.html') @app.route('/login', methods=['GET', 'POST']) def login(): error = None print(request.method) if request.method == 'POST': if request.form['username'] != 'admin' or \ request.form['password'] != 'admin': error = 'Invalid username or password. Please try again!' else: #flash('You have successfully logged in') flash('You were successfully logged in') return redirect(url_for('index')) return render_template('login.html', error=error) if __name__ == "__main__": app.run(debug=True)
After executing the above code, you will see the screen as shown below.
When you click the link, you will be redirected to the login page. Enter your username and password -
Click LoginButton. A message 'You have successfully logged in' will be displayed.