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
Provenance
- Commit
a9d4ea8- Tree
95e5ffb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
727c701
2 files changed+86 added−68 removed
modified:docs/patterns.rst+82 added
162 unmodified lines163 this template "extends" another template. When the template system evaluates164 this template, first it locates the parent. The extends tag must be the165 first tag in the template. To render the contents of a block defined in166 the parent template, use ``{{ super() }}``.167+168+.. _message-flashing-pattern:169+170+Message Flashing171+----------------172+173+Good applications and user interfaces are all about feedback. If the user174+does not get enough feedback he will probably end up hating the175+application. Flask provides a really simple way to give feedback to a176+user with the flashing system. The flashing system basically makes it177+possible to record a message at the end of a request and access it next178+request and only next request. This is usually combined with a layout179+template that does this.180+181+So here a full example::182+183+ from flask import flash, redirect, url_for, render_template184+185+ @app.route('/')186+ def index():187+ return render_template('index.html')188+189+ @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)200+201+And here the ``layout.html`` template which does the magic:202+203+.. sourcecode:: html+jinja204+205+ <!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 %}217+218+And here the index.html template:219+220+.. sourcecode:: html+jinja221+222+ {% 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 %}227+228+And of course the login template:229+230+.. sourcecode:: html+jinja231+232+ {% 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 lines488 possible to record a message at the end of a request and access it next489 request and only next request. This is usually combined with a layout490 template that does this.491 492-So here a full example::493-494- from flask import flash, redirect, url_for, render_template495-496- @app.route('/')497- def index():498- return render_template('index.html')499-500- @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)511-512-And here the ``layout.html`` template which does the magic:513-514-.. sourcecode:: html+jinja515-516- <!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 %}528-529-And here the index.html template:530-531-.. sourcecode:: html+jinja532-533- {% 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 %}538-539-And of course the login template:540-541-.. sourcecode:: html+jinja542-543- {% 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 %}492+To flash a message use the :func:`~flask.flash` method, to get hold of the493+messages you can use :func:`~flask.get_flashed_messages` which is also494+available in the templates. Check out the :ref:`message-flashing-pattern`495+for a full example.