flashing.rst
This file
Path
docs/patterns/flashing.rst
150 lines · 4.5 KiB
Last changed by
Merge pull request #1851 from prachikhadke/1789-document-flash-limitJeff Widmanbefore adoption5cf261a
History of this fileTake it
The walk stopped after 200 commits: lines older than 41b5d77 point at it.
Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 1–11why, one step back1.. _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 layoutMerge pull request #1851 from prachikhadke/1789-document-flash-limitJeff Widmanbefore adoption5cf261a
lines 12–14why, one step back12template that does this. Note that browsers and sometimes web servers enforce13a limit on cookie sizes. This means that flashing messages that are too14large for session cookies causes message flashing to fail silently.Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 15–42why, one step back1516Simple Flashing17---------------1819So here is a full example::2021 from flask import Flask, flash, redirect, render_template, \22 request, url_for2324 app = Flask(__name__)25 app.secret_key = 'some_secret'2627 @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)42Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1
line 43why, one step back43And here is the :file:`layout.html` template which does the magic:Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 44–59why, one step back4445.. 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 %}59Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1
line 60why, one step back60Here is the :file:`index.html` template which inherits from :file:`layout.html`:Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 61–69why, one step back6162.. 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 %}69Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1
lines 70–71why, one step back70And here is the :file:`login.html` template which also inherits from71:file:`layout.html`:Merge pull request #1273 from prakashpp/patch-1Markus Unterwaditzerbefore adoption41b5d77
lines 72–150why, one step back7273.. 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 %}81 <form action="" method=post>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 %}