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 ...

Get Learning Flask Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.