Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using flash messages


When a user performs an action on a site, it is common to display a one-time message on the subsequent page-load indicating that their action has succeeded. These are called flash messages and Flask comes with a helper for displaying them. In order to get started using flash messages, we need to take a brief detour to our config module where we will be adding a secret key. The secret key is necessary because flash messages are stored in the session, which in turn is stored as an encrypted cookie. To securely encrypt this data, Flask needs a key.

Open config.py and add a secret key. It can be a phrase, random characters, whatever you like:

class Configuration(object):
    APPLICATION_DIR = current_directory
    DEBUG = True
    SECRET_KEY = 'flask is fun!'  # Create a unique key for your app.
    SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/blog.db' % APPLICATION_DIR

Now, wherever we have the user performing an action, we want to flash them a message indicating that their action...