Flask flash() method – How to Flash Messages in Flask?

Flask Flash

In this tutorial, we will learn how to flash messages using the Flask flash() method.

What does it mean to flash a message?

It is always good for GUI applications to give feedback to the user for his actions.

That is, for example, in Flask form, if the user leaves a field empty, it is appreciable to give him an error/info message telling that the field is required.

The Python Tkinter GUI framework uses the message or the dialogue box to show the message, and in Client-Side Scripting Language JavaScript, it is done using the alert function.

And in Flask, we have this flash method to do precisely that.

How does Flask flash work?

The Flask flash method shows messages to the users.

With Flash, we can create a flash message in one Flask View and then show it in another View, called “next,” which usually is a template View.

A typical example of Template View is:

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

Hence, a Flask view creates a Flash message in one view and then passes it to the next view( along with the request), which displays the message to the user.

The syntax for Flash:

flash(message,category)

Here,

  • message: The message to display
  • category: An Optional parameter, which can be set to “error,” “info,” or “warning.”

To extract the flash message from the session, where it is stored, and display it on the template, we use the get_flashed_messages() function.

get_flashed_messages(with_categories, category_filter)

Here,

  • with_categories: An Optional Parameter tuple to mention the category(error/info/warning)
  • category_filter: An Optional Parameter to filter and display only specific messages

A simple example showing the get_flashed_message() in Template file:

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

Hands-On with Flask flash() method

Here we will create a simple Flask application that flashes – login successful if the user enters the correct password.

1) Coding the Flask Application File

Here we will show a simple form taking in a password. If the password is correct then flash the message.

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.

That’s it !! let us now code the Templates

2) Coding the Templates

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>

And the Success.html template file:

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

<h2>User Authenticated</h2>

Notice how we used the get_flashed_messages() here.

3) Implementation

That’s it !! Let us now fire up the server and check our webpage

Hit “/form”:

Form
Form

Enter 1234 and hit submit

Login
Login

And there it is, Our Flash message right on the screen.

Conclusion

That’s it for this tutorial, guys!! I hope you gained enough knowledge to use the Flash method in your web applications. Do check out our Flask Forms article to know more about Forms

See you guys in the next article !! Happy Coding !!