Introduction

We will see here how to do flash message management using Python, Flask. Ideally almost every application needs to provide some meaningful message to end user after some activities performed by end users. Such activities may be registration on a site, login, payment, etc. So after each operation whether success or failure most of the time we need to provide meaningful message so that end user will understand what happens or what would be the next step.

Flask provides a really simple way to give feedback to a user with the flashing system. The flashing system basically makes it possible to record a message at the end of a request and access it next request and only next request. This is usually combined with a layout template that does this.

Prerequisites

Python 3.7.4, Flask 1.1.1, Windows 10 64 bit

Configuring Flask

Create below configuration before you use session or flash from Flask framework.

The secret key ideally should be the encrypted one.

from flask import Flask

app = Flask(__name__)
app.secret_key = "secret key"

Putting Message into flash

Simple flash

We will put simple message into flash. We render a simple template.

@app.route('/')
def app_session():
	flash('This is a flash message')
	return render_template('template.html')

Flash with Category

It is possible to provide category when flashing a message. The default category if nothing is provided is message.

For example, error messages could be displayed with a red background and success messages could be displayed with a green background .

@app.route('/')
def app_session():
	flash('This is a flash error message', 'error')
	flash('This is a flash success message', 'success')
	return render_template('template.html')

Filter Message

Optionally you can pass a list of categories which filters the results of get_flashed_messages(). This is useful if you wish to render each category in a separate block.

@app.route('/')
def app_session():
	flash('This is a flash error message', 'error')
	flash('This is a flash success message', 'success')
	return render_template('template.html')

Retrieving Messages from flash

Simple flash

To retrieve message or feedback from flash simply use similar to below code into the template file and display:

{% with messages = get_flashed_messages() %}
  {% if messages %}
	<ul>
	{% for message in messages %}
	  <li>{{ message }}</li>
	{% endfor %}
	</ul>
  {% endif %}
{% endwith %}

In the above code snippets, the important part is get_flashed_messages() that retrieves all messages from flash.

Later you can use your own way to display the feedbacks or messages.

Output on Browser
session management in python flask

Flash with Category

The categorical flash messages could be retrieved in the following way:

<div>
	{% with messages = get_flashed_messages(with_categories=true) %}
	  {% if messages %}
		<ul>
		{% for category, message in messages %}
		  <li class="{{ category }}">{{ message }}</li>
		{% endfor %}
		</ul>
	  {% endif %}
	{% endwith %}
</div>
Output on Browser
session management in python flask

Filter Message

For example, we are filter messages which have category error.

<div>
	{% with errors = get_flashed_messages(category_filter=["error"]) %}
	  {% if errors %}
		<ul>
		{%- for msg in errors %}
			<li>{{ msg }}</li>
		{% endfor -%}
		</ul>
	  {% endif %}
	{% endwith %}
</div>
Output on Browser
session management in python flask

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *