flashing.rst
This file
Path
docs/patterns/flashing.rst
119 lines · 3.5 KiB
Last changed by
0.5 is 0.3 now, why skip numbers?Armin Ronacherbefore adoptionce6e4cb
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 he 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 a full example::1819 from flask import flash, redirect, url_for, render_template2021 @app.route('/')22 def index():23 return render_template('index.html')2425 @app.route('/login', methods=['GET', 'POST'])26 def login():27 error = None28 if request.method == 'POST':29 if request.form['username'] != 'admin' or \30 request.form['password'] != 'secret':31 error = 'Invalid credentials'32 else:33 flash('You were sucessfully logged in')34 return redirect(url_for('index'))35 return render_template('login.html', error=error)3637And here the ``layout.html`` template which does the magic:3839.. sourcecode:: html+jinja4041 <!doctype html>42 <title>My Application</title>43 {% with messages = get_flashed_messages() %}44 {% if messages %}45 <ul class=flashes>46 {% for message in messages %}47 <li>{{ message }}</li>48 {% endfor %}49 </ul>50 {% endif %}51 {% endwith %}52 {% block body %}{% endblock %}5354And here the index.html template:5556.. sourcecode:: html+jinja5758 {% extends "layout.html" %}59 {% block body %}60 <h1>Overview</h1>61 <p>Do you want to <a href="{{ url_for('login') }}">log in?</a>62 {% endblock %}6364And of course the login template:6566.. sourcecode:: html+jinja6768 {% extends "layout.html" %}69 {% block body %}70 <h1>Login</h1>71 {% if error %}72 <p class=error><strong>Error:</strong> {{ error }}73 {% endif %}74 <form action="" method=post>75 <dl>76 <dt>Username:77 <dd><input type=text name=username value="{{78 request.form.username }}">79 <dt>Password:80 <dd><input type=password name=password>81 </dl>82 <p><input type=submit value=Login>83 </form>84 {% endblock %}8586Flashing With Categories87------------------------8889.. versionadded:: 0.39091It is also possible to provide categories when flashing a message. The92default category if nothing is provided is ``'message'``. Alternative93categories can be used to give the user better feedback. For example94error messages could be displayed with a red background.9596To flash a message with a different category, just use the second argument97to the :func:`~flask.flash` function::9899 flash(u'Invalid password provided', 'error')100101Inside the template you then have to tell the102:func:`~flask.get_flashed_messages` function to also return the103categories. The loop looks slighty different in that situation then:104105.. sourcecode:: html+jinja106107 {% with messages = get_flashed_messages(with_categories=true) %}108 {% if messages %}109 <ul class=flashes>110 {% for category, message in messages %}111 <li class="{{ category }}">{{ message }}</li>112 {% endfor %}113 </ul>114 {% endif %}115 {% endwith %}116117This is just one example of how to render these flashed messages. One118might also use the category to add a prefix such as119``<strong>Error:</strong>`` to the message.