Message Flashing
So here is a full example:
And here is the template which does the magic:
- <!doctype html>
- {% with messages = get_flashed_messages() %}
- {% if messages %}
- <ul class=flashes>
- {% for message in messages %}
- {% endfor %}
- </ul>
- {% endif %}
- {% endwith %}
Here is the index.html
template which inherits from layout.html
:
And here is the template which also inherits fromlayout.html
:
{% extends "layout.html" %} {% block body %} <h1>Login</h1> {% if error %} <p class=error><strong>Error:</strong> {{ error }} {% endif %} <form method=post> <dl> <dt>Username: <dd><input type=text name=username value="{{ request.form.username }}"> <dt>Password: <dd><input type=password name=password> </dl> <p><input type=submit value=Login> </form> {% endblock %}
Changelog
New in version 0.3.
'message'
. Alternativecategories can be used to give the user better feedback. For exampleerror messages could be displayed with a red background.
To flash a message with a different category, just use the second argumentto the function:
Inside the template you then have to tell the function to also return thecategories. The loop looks slightly different in that situation then:
{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} <ul class=flashes> {% for category, message in messages %} <li class="{{ category }}">{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %}
Changelog
New in version 0.9.