Flask Tutorials: Flash Messages (flash and get_flashed_messages)

beginner
16 min

Flask Tutorials: Flash Messages (flash and get_flashed_messages)

Welcome back to CodeYourCraft! Today, we're going to dive into a powerful feature of Flask known as Flash Messages. This tool helps you communicate important information to the user during their interaction with your web application. Let's get started! 🎯

What are Flash Messages?

Flash Messages are a way to display temporary messages to the user in a web application. They are particularly useful when you want to inform the user about successful or unsuccessful actions without redirecting them to a different page. 📝

Installing Flask

Before we dive into Flash Messages, let's make sure you have Flask installed. If you haven't already, you can install it using pip:

pip install flask

Creating a Basic Flask Application

Let's create a simple Flask application to understand Flash Messages better.

python
from flask import Flask, flash, request app = Flask(__name__) @app.route('/') def home(): return "Welcome to CodeYourCraft!" if __name__ == '__main__': app.run(debug=True)

In this example, we've created a basic Flask application that displays a simple message when you access the home page. But what if we want to show a custom message depending on user actions? That's where Flash Messages come in!

Using Flash Messages

Flask provides a flash() function for adding messages to the user session. When a message is flashed, it can be retrieved and displayed by using get_flashed_messages().

Let's modify our previous example to demonstrate this:

python
from flask import Flask, flash, request, render_template app = Flask(__name__) @app.route('/') def home(): if request.args.get('message'): flash(request.args.get('message'), 'success') return render_template('home.html') @app.route('/set_message') def set_message(): return render_template('set_message.html', message="Hello, World!") @app.route('/') def home_with_message(): messages = get_flashed_messages(with_categories=True) return render_template('home.html', messages=messages) if __name__ == '__main__': app.run(debug=True)

In this modified example, we've added two new routes: /set_message and /. The /set_message route sets a message and returns a template for it. The / route checks if a message has been set and displays it if it exists.

Let's create the templates for set_message and home:

html
<!-- set_message.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Set Message</title> </head> <body> <a href="/">Go to Home</a> </body> </html>
html
<!-- home.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>Welcome to CodeYourCraft!</h1> {% for message in messages %} <div class="alert alert-success">{{ message.message }}</div> {% endfor %} </body> </html>

In the templates, we're using Bootstrap's alert component to display the messages. The {% for message in messages %} loop iterates over the messages and displays each one.

Now, when you access the /set_message route, a message is set, and when you go to the home page, you'll see the message displayed. 💡 Pro Tip: You can customize the message type by passing a category (e.g., 'success', 'warning', 'error') to the flash() function.

Quiz

Quick Quiz
Question 1 of 1

Which function is used to add a Flash Message in Flask?

With this, we've covered the basics of Flash Messages in Flask. In the next tutorial, we'll delve deeper into using Flash Messages for user registration and login scenarios. Stay tuned! 🎯

Remember, practice makes perfect! Play around with the code examples and try adding your own messages to the application. Happy coding! 🚀