flashing.rst
This file
Path
docs/patterns/flashing.rst
148 lines · 4.4 KiB
Last changed by
Not known.
History of this fileTake it
1Message Flashing2================34Good applications and user interfaces are all about feedback. If the user5does not get enough feedback they will probably end up hating the6application. Flask provides a really simple way to give feedback to a7user with the flashing system. The flashing system basically makes it8possible to record a message at the end of a request and access it next9request and only next request. This is usually combined with a layout10template that does this. Note that browsers and sometimes web servers enforce11a limit on cookie sizes. This means that flashing messages that are too12large for session cookies causes message flashing to fail silently.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 = b'_5#y2L"F4Q8z\n\xec]/'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 is the :file:`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 %}5758Here is the :file:`index.html` template which inherits from :file:`layout.html`: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 here is the :file:`login.html` template which also inherits from69:file:`layout.html`:7071.. sourcecode:: html+jinja7273 {% extends "layout.html" %}74 {% block body %}75 <h1>Login</h1>76 {% if error %}77 <p class=error><strong>Error:</strong> {{ error }}78 {% endif %}79 <form method=post>80 <dl>81 <dt>Username:82 <dd><input type=text name=username value="{{83 request.form.username }}">84 <dt>Password:85 <dd><input type=password name=password>86 </dl>87 <p><input type=submit value=Login>88 </form>89 {% endblock %}9091Flashing With Categories92------------------------9394.. versionadded:: 0.39596It is also possible to provide categories when flashing a message. The97default category if nothing is provided is ``'message'``. Alternative98categories can be used to give the user better feedback. For example99error messages could be displayed with a red background.100101To flash a message with a different category, just use the second argument102to the :func:`~flask.flash` function::103104 flash('Invalid password provided', 'error')105106Inside the template you then have to tell the107:func:`~flask.get_flashed_messages` function to also return the108categories. The loop looks slightly different in that situation then:109110.. sourcecode:: html+jinja111112 {% with messages = get_flashed_messages(with_categories=true) %}113 {% if messages %}114 <ul class=flashes>115 {% for category, message in messages %}116 <li class="{{ category }}">{{ message }}</li>117 {% endfor %}118 </ul>119 {% endif %}120 {% endwith %}121122This is just one example of how to render these flashed messages. One123might also use the category to add a prefix such as124``<strong>Error:</strong>`` to the message.125126Filtering Flash Messages127------------------------128129.. versionadded:: 0.9130131Optionally you can pass a list of categories which filters the results of132:func:`~flask.get_flashed_messages`. This is useful if you wish to133render each category in a separate block.134135.. sourcecode:: html+jinja136137 {% with errors = get_flashed_messages(category_filter=["error"]) %}138 {% if errors %}139 <div class="alert-message block-message error">140 <a class="close" href="#">×</a>141 <ul>142 {%- for msg in errors %}143 <li>{{ msg }}</li>144 {% endfor -%}145 </ul>146 </div>147 {% endif %}148 {% endwith %}