Skip to content
Myr

flashing.rst

This file

Path

docs/patterns/flashing.rst

150 lines · 4.5 KiB

Last changed by

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

History of this file

Take it

The walk stopped after 200 commits: lines older than f6618c7 point at it.

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 1–11why, one step back
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 layout

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 12–14why, one step back
12template that does this. Note that browsers and sometimes web servers enforce
13a limit on cookie sizes. This means that flashing messages that are too14large for session cookies causes message flashing to fail silently.

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 15–24why, one step back
15
16Simple Flashing17---------------1819So here is a full example::2021 from flask import Flask, flash, redirect, render_template, \22 request, url_for2324 app = Flask(__name__)

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 25why, one step back
25 app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 26–42why, one step back
26
27 @app.route('/')28 def index():29 return render_template('index.html')3031 @app.route('/login', methods=['GET', 'POST'])32 def login():33 error = None34 if request.method == 'POST':35 if request.form['username'] != 'admin' or \36 request.form['password'] != 'secret':37 error = 'Invalid credentials'38 else:39 flash('You were successfully logged in')40 return redirect(url_for('index'))41 return render_template('login.html', error=error)42

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

line 43why, one step back
43And here is the :file:`layout.html` template which does the magic:

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 44–59why, one step back
44
45.. sourcecode:: html+jinja4647 <!doctype html>48 <title>My Application</title>49 {% with messages = get_flashed_messages() %}50 {% if messages %}51 <ul class=flashes>52 {% for message in messages %}53 <li>{{ message }}</li>54 {% endfor %}55 </ul>56 {% endif %}57 {% endwith %}58 {% block body %}{% endblock %}59

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

line 60why, one step back
60Here is the :file:`index.html` template which inherits from :file:`layout.html`:

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 61–69why, one step back
61
62.. sourcecode:: html+jinja6364 {% extends "layout.html" %}65 {% block body %}66 <h1>Overview</h1>67 <p>Do you want to <a href="{{ url_for('login') }}">log in?</a>68 {% endblock %}69

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

lines 70–71why, one step back
70And here is the :file:`login.html` template which also inherits from
71:file:`layout.html`:

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 72–80why, one step back
72
73.. sourcecode:: html+jinja7475 {% extends "layout.html" %}76 {% block body %}77 <h1>Login</h1>78 {% if error %}79 <p class=error><strong>Error:</strong> {{ error }}80 {% endif %}

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 81why, one step back
81 <form method=post>

Merge pull request #1099 from root-11/patch-1Daniel Neuhäuserbefore adoptionf6618c7

lines 82–150why, one step back
82 <dl>
83 <dt>Username:84 <dd><input type=text name=username value="{{85 request.form.username }}">86 <dt>Password:87 <dd><input type=password name=password>88 </dl>89 <p><input type=submit value=Login>90 </form>91 {% endblock %}9293Flashing With Categories94------------------------9596.. versionadded:: 0.39798It is also possible to provide categories when flashing a message. The99default category if nothing is provided is ``'message'``. Alternative100categories can be used to give the user better feedback. For example101error messages could be displayed with a red background.102103To flash a message with a different category, just use the second argument104to the :func:`~flask.flash` function::105106 flash(u'Invalid password provided', 'error')107108Inside the template you then have to tell the109:func:`~flask.get_flashed_messages` function to also return the110categories. The loop looks slightly different in that situation then:111112.. sourcecode:: html+jinja113114 {% with messages = get_flashed_messages(with_categories=true) %}115 {% if messages %}116 <ul class=flashes>117 {% for category, message in messages %}118 <li class="{{ category }}">{{ message }}</li>119 {% endfor %}120 </ul>121 {% endif %}122 {% endwith %}123124This is just one example of how to render these flashed messages. One125might also use the category to add a prefix such as126``<strong>Error:</strong>`` to the message.127128Filtering Flash Messages129------------------------130131.. versionadded:: 0.9132133Optionally you can pass a list of categories which filters the results of134:func:`~flask.get_flashed_messages`. This is useful if you wish to135render each category in a separate block.136137.. sourcecode:: html+jinja138139 {% with errors = get_flashed_messages(category_filter=["error"]) %}140 {% if errors %}141 <div class="alert-message block-message error">142 <a class="close" href="#">×</a>143 <ul>144 {%- for msg in errors %}145 <li>{{ msg }}</li>146 {% endfor -%}147 </ul>148 </div>149 {% endif %}150 {% endwith %}