Skip to content
Myr

Flashing -> patterns documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 727c701 · Browse files · 2 files+86 added−68 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+86 added−68 removed

  • other:+86 added−68 removed

2 files+86 added−68 removed

modified:docs/patterns.rst+82 added
162 unmodified lines
163this template "extends" another template. When the template system evaluates164this template, first it locates the parent. The extends tag must be the165first tag in the template. To render the contents of a block defined in166the parent template, use ``{{ super() }}``.167168.. _message-flashing-pattern:169170Message Flashing171----------------172173Good applications and user interfaces are all about feedback. If the user174does not get enough feedback he will probably end up hating the175application. Flask provides a really simple way to give feedback to a176user with the flashing system. The flashing system basically makes it177possible to record a message at the end of a request and access it next178request and only next request. This is usually combined with a layout179template that does this.180181So here a full example::182183 from flask import flash, redirect, url_for, render_template184185 @app.route('/')186 def index():187 return render_template('index.html')188189 @app.route('/login', methods=['GET', 'POST'])190 def login():191 error = None192 if request.method == 'POST':193 if request.form['username'] != 'admin' or \194 request.form['password'] != 'secret':195 error = 'Invalid credentials'196 else:197 flash('You were sucessfully logged in')198 return redirect(url_for('index'))199 return render_template('login.html', error=error)200201And here the ``layout.html`` template which does the magic:202203.. sourcecode:: html+jinja204205 <!doctype html>206 <title>My Application</title>207 {% with messages = get_flashed_messages() %}208 {% if messages %}209 <ul class=flashes>210 {% for message in messages %}211 <li>{{ message }}</li>212 {% endfor %}213 </ul>214 {% endif %}215 {% endwith %}216 {% block body %}{% endblock %}217218And here the index.html template:219220.. sourcecode:: html+jinja221222 {% extends "layout.html" %}223 {% block body %}224 <h1>Overview</h1>225 <p>Do you want to <a href="{{ url_for('login') }}">log in?</a>226 {% endblock %}227228And of course the login template:229230.. sourcecode:: html+jinja231232 {% extends "layout.html" %}233 {% block body %}234 <h1>Login</h1>235 {% if error %}236 <p class=error><strong>Error:</strong> {{ error }}237 {% endif %}238 <form action="" method=post>239 <dl>240 <dt>Username:241 <dd><input type=text name=username value="{{242 request.form.username }}">243 <dt>Password:244 <dd><input type=password name=password>245 </dl>246 <p><input type=submit value=Login>247 </form>248 {% endblock %}
modified:docs/quickstart.rst+4 added-68 removed
487 unmodified lines
488possible to record a message at the end of a request and access it next489request and only next request. This is usually combined with a layout490template that does this.491492So here a full example::493494 from flask import flash, redirect, url_for, render_template495496 @app.route('/')497 def index():498 return render_template('index.html')499500 @app.route('/login', methods=['GET', 'POST'])501 def login():502 error = None503 if request.method == 'POST':504 if request.form['username'] != 'admin' or \505 request.form['password'] != 'secret':506 error = 'Invalid credentials'507 else:508 flash('You were sucessfully logged in')509 return redirect(url_for('index'))510 return render_template('login.html', error=error)511512And here the ``layout.html`` template which does the magic:513514.. sourcecode:: html+jinja515516 <!doctype html>517 <title>My Application</title>518 {% with messages = get_flashed_messages() %}519 {% if messages %}520 <ul class=flashes>521 {% for message in messages %}522 <li>{{ message }}</li>523 {% endfor %}524 </ul>525 {% endif %}526 {% endwith %}527 {% block body %}{% endblock %}528529And here the index.html template:530531.. sourcecode:: html+jinja532533 {% extends "layout.html" %}534 {% block body %}535 <h1>Overview</h1>536 <p>Do you want to <a href="{{ url_for('login') }}">log in?</a>537 {% endblock %}538539And of course the login template:540541.. sourcecode:: html+jinja542543 {% extends "layout.html" %}544 {% block body %}545 <h1>Login</h1>546 {% if error %}547 <p class=error><strong>Error:</strong> {{ error }}548 {% endif %}549 <form action="" method=post>550 <dl>551 <dt>Username:552 <dd><input type=text name=username value="{{553 request.form.username }}">554 <dt>Password:555 <dd><input type=password name=password>556 </dl>557 <p><input type=submit value=Login>558 </form>559 {% endblock %}492To flash a message use the :func:`~flask.flash` method, to get hold of the493messages you can use :func:`~flask.get_flashed_messages` which is also494available in the templates. Check out the :ref:`message-flashing-pattern`495for a full example.