flashing.rst
This file
Path
docs/patterns/flashing.rst
147 lines · 4.2 KiB
Last changed by
Updated documentation once more for new cli.Armin Ronacherbefore adoptiona3ad540
History of this fileTake it
1.. _message-flashing-pattern:23Message Flashing4================56Good applications and user interfaces are all about feedback. If the user7does not get enough feedback they will probably end up hating the8application. Flask provides a really simple way to give feedback to a9user with the flashing system. The flashing system basically makes it10possible to record a message at the end of a request and access it next11request and only next request. This is usually combined with a layout12template that does this.1314Simple Flashing15---------------1617So here is a full example::1819 from flask import Flask, flash, redirect, render_template, \20 request, url_for2122 app = Flask(__name__)23 app.secret_key = 'some_secret'2425 @app.route('/')26 def index():27 return render_template('index.html')2829 @app.route('/login', methods=['GET', 'POST'])30 def login():31 error = None32 if request.method == 'POST':33 if request.form['username'] != 'admin' or \34 request.form['password'] != 'secret':35 error = 'Invalid credentials'36 else:37 flash('You were successfully logged in')38 return redirect(url_for('index'))39 return render_template('login.html', error=error)4041And here the ``layout.html`` template which does the magic:4243.. sourcecode:: html+jinja4445 <!doctype html>46 <title>My Application</title>47 {% with messages = get_flashed_messages() %}48 {% if messages %}49 <ul class=flashes>50 {% for message in messages %}51 <li>{{ message }}</li>52 {% endfor %}53 </ul>54 {% endif %}55 {% endwith %}56 {% block body %}{% endblock %}5758And here the index.html template:5960.. sourcecode:: html+jinja6162 {% extends "layout.html" %}63 {% block body %}64 <h1>Overview</h1>65 <p>Do you want to <a href="{{ url_for('login') }}">log in?</a>66 {% endblock %}6768And of course the login template:6970.. sourcecode:: html+jinja7172 {% extends "layout.html" %}73 {% block body %}74 <h1>Login</h1>75 {% if error %}76 <p class=error><strong>Error:</strong> {{ error }}77 {% endif %}78 <form action="" method=post>79 <dl>80 <dt>Username:81 <dd><input type=text name=username value="{{82 request.form.username }}">83 <dt>Password:84 <dd><input type=password name=password>85 </dl>86 <p><input type=submit value=Login>87 </form>88 {% endblock %}8990Flashing With Categories91------------------------9293.. versionadded:: 0.39495It is also possible to provide categories when flashing a message. The96default category if nothing is provided is ``'message'``. Alternative97categories can be used to give the user better feedback. For example98error messages could be displayed with a red background.99100To flash a message with a different category, just use the second argument101to the :func:`~flask.flash` function::102103 flash(u'Invalid password provided', 'error')104105Inside the template you then have to tell the106:func:`~flask.get_flashed_messages` function to also return the107categories. The loop looks slightly different in that situation then:108109.. sourcecode:: html+jinja110111 {% with messages = get_flashed_messages(with_categories=true) %}112 {% if messages %}113 <ul class=flashes>114 {% for category, message in messages %}115 <li class="{{ category }}">{{ message }}</li>116 {% endfor %}117 </ul>118 {% endif %}119 {% endwith %}120121This is just one example of how to render these flashed messages. One122might also use the category to add a prefix such as123``<strong>Error:</strong>`` to the message.124125Filtering Flash Messages126------------------------127128.. versionadded:: 0.9129130Optionally you can pass a list of categories which filters the results of131:func:`~flask.get_flashed_messages`. This is useful if you wish to132render each category in a separate block.133134.. sourcecode:: html+jinja135136 {% with errors = get_flashed_messages(category_filter=["error"]) %}137 {% if errors %}138 <div class="alert-message block-message error">139 <a class="close" href="#">×</a>140 <ul>141 {%- for msg in errors %}142 <li>{{ msg }}</li>143 {% endfor -%}144 </ul>145 </div>146 {% endif %}147 {% endwith %}