Flask flash() method


Flash Messages using Flask. Flash messages serve as a form of signal that informs users about a consequence of their actions or inaction especially on platforms that make use of GUI applications.

This article will provide a clear insight into how to make use of the Flask flash() method to create flash messages.

A glaring case study of how flash messages work is if a user is signing up on a page but fails to fill a certain box, the flash message notifies the user that it is necessary for him or her to fill in the required field.

Related course: Create Web Apps with Python Flask

Flash messages

Flash messages are reflected in form of alerts when programs using client-side Javascript while a message pop up or dialogue box to display the error message. The flash method is what we use in the flask to execute this effect.

How the flash method works in Flask?

When the necessary conditions apply, the flash method displays a message to the users. A flash message can be programmed in a flask view to be implemented in another view tagged "Next". This forms a template view which can be seen as:

@app.route('/template')
def blog():
    #codes...
    #codes...
    return render_template('template.html')

The method works efficiently by passing along the message to the next page where the user comes across it. The syntax is supposed to be like this for Flash messages:

flash(message,category)

In this syntax, Message: This is the text to be shown to the user.

Category: This represents the scope of the message whether it represents an error, necessary information, or a warning.

In order to facilitate the proper run of the flash message code, it needs to be extracted and displayed in the template. Hence, we make use of the get_flashed_messages() function.

get_flashed_messages(with_categories, category_filter)

In this syntax, with_categories: This is an optional classification that defines the message as an error, information, or a warning. category_filter: This filters the message into its possible categories and displays the flash message in a characteristic way to the error.

Here is a template reflecting a message derived using the get_flashed_message() function:

{% with messages = get_flashed_messages() %}
   {% if messages %}
      {% for message in messages %}
         {{ message }}
      {% endfor %}
   {% endif %}
{% endwith %}

Practical ways to execute the Flask flash() method

Below, you will see how to generate a flash app that displays a message login successfully if a user correctly enters his login details. 1) Writing the code for the Flask Application File

Below, you can see a code of a sign in the form requiring a password. It is programmed that a flash message should appear if the password is entered correctly.

from flask import Flask, render_template, request, redirect, flash
app = Flask(__name__)
@app.route('/form')
def form():
    return render_template('form.html')
@app.route('/login', methods = ['POST', 'GET'])
def login():
    if request.method == 'GET':
        return "Login via the login Form"

    if request.method == 'POST':
        password = request.form['password']
        if password == '123':
            #The following flash message will be displayed on successful login
            flash('Login successful')
            return render_template('success.html')
        else:
            return redirect('/form')
app.run(host='localhost', port=5000)

Here the success.html is the “next” Template since the message will appear there. Next is to write the template codes.

2) Creating the Templates The code for the form.html will be:

<form action="/login" method = "POST">
   <p>password <input type = "text" name = "password" /></p>
   <p><input type = "submit" value = "Submit" /></p>
</form>

Here is the Success.html template file:

{% with messages = get_flashed_messages() %}
   {% if messages %}
      {% for message in messages %}
         {{ message }}
      {% endfor %}
   {% endif %}
{% endwith %}
<h2>User Authenticated</h2>

Did you observe how we used the get_flashed_messages() function here?

3) Running the code Finally, all that is next is to test the code and launch the website to log in and enter the password.

Hit “/form”: Form Form Enter the password, "1234” and hit submit. Login Login

The flash message will definitely once the correct password is entered in the field.

Conclusion

Creating a flash message using the flask method is as simple as that. You can easily deploy this in writing codes for your web applications. To learn more about the flask method, read our other articles. See you next time.

Related course: Create Web Apps with Python Flask