Skip to content
Myr

flashing.rst

This file

Path

docs/patterns/flashing.rst

148 lines · 4.3 KiB

Last changed by

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

History of this file

Take it

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

Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01

lines 1–40why, 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 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)40

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

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

Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01

lines 42–57why, one step back
42
43.. 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 %}57

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

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

Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01

lines 59–67why, one step back
59
60.. 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 %}67

Merge pull request #1354 from idrisr/masterMarkus Unterwaditzerbefore adoptionc282ab1

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

Merge pull request #1132 from nicorevin/update_linksDaniel Neuhäuserbefore adoption8d3ee01

lines 70–148why, one step back
70
71.. 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 action="" 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(u'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 %}