メッセージのフラッシュ表示(Message Flashing) Message Flashing¶
良いアプリケーションおよびユーザインタフェースとは、すべてフィードバック次第です。もしユーザが十分なフィードバックを得られない場合、おそらくアプリケーションを嫌いになって終わりです。Flaskはフラッシュ表示の仕組み(flashing system)によって、ユーザへフィードバックを与える非常にシンプルな方法を提供します。フラッシュ表示の仕組みは、リクエストの最後にメッセージを記録し、次のそしてただ次だけのリクエストでアクセスできるようにします。これは、普通はこれ(訳注: メッセージのフラッシュ表示)をするレイアウトのテンプレートと組み合わされます。ブラウザとときにはwebサーバがcookieのサイズに制限を課すことに注意してください。これは、セッションのcookieに対して大き過ぎるフラッシュ表示用のメッセージでは、何も反応せずにメッセージのフラッシュ表示が失敗することを意味します。 Good applications and user interfaces are all about feedback. If the user does not get enough feedback they will probably end up hating the application. Flask provides a really simple way to give feedback to a user with the flashing system. The flashing system basically makes it possible to record a message at the end of a request and access it next request and only next request. This is usually combined with a layout template that does this. Note that browsers and sometimes web servers enforce a limit on cookie sizes. This means that flashing messages that are too large for session cookies causes message flashing to fail silently.
シンプルなフラッシュ表示 Simple Flashing¶
それでは、以下は一通りの例(full example)です: So here is a full example::
from flask import Flask, flash, redirect, render_template, \
request, url_for
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or \
request.form['password'] != 'secret':
error = 'Invalid credentials'
else:
flash('You were successfully logged in')
return redirect(url_for('index'))
return render_template('login.html', error=error)
そして、以下は魔法をかけるlayout.html
テンプレートです:
And here is the :file:`layout.html` template which does the magic:
<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block body %}{% endblock %}
以下はlayout.html
を継承したindex.html
テンプレートです:
Here is the :file:`index.html` template which inherits from :file:`layout.html`:
{% extends "layout.html" %}
{% block body %}
<h1>Overview</h1>
<p>Do you want to <a href="{{ url_for('login') }}">log in?</a>
{% endblock %}
そして、以下はlayout.html
を継承したlogin.html
テンプレートです:
And here is the :file:`login.html` template which also inherits from :file:`layout.html`:
{% extends "layout.html" %}
{% block body %}
<h1>Login</h1>
{% if error %}
<p class=error><strong>Error:</strong> {{ error }}
{% endif %}
<form method=post>
<dl>
<dt>Username:
<dd><input type=text name=username value="{{
request.form.username }}">
<dt>Password:
<dd><input type=password name=password>
</dl>
<p><input type=submit value=Login>
</form>
{% endblock %}
カテゴリーを使ったフラッシュ表示 Flashing With Categories¶
Changelog
バージョン 0.3 で追加.
メッセージをフラッシュ表示するときにカテゴリーを提供することも可能です。(カテゴリーに)何も提供されなかった時の既定のカテゴリーは'message'
です。ユーザーにより良いフィードバックを与えるために代わりのカテゴリーを使うことができます。例えば、エラーメッセージを赤い背景色を使って表示できます。
It is also possible to provide categories when flashing a message. The default category if nothing is provided is ``'message'``. Alternative categories can be used to give the user better feedback. For example error messages could be displayed with a red background.
異なるカテゴリーを使ってメッセージをフラッシュ表示するには、ただ2番目の引数をflash()
関数で使うだけです:
To flash a message with a different category, just use the second argument to the :func:`~flask.flash` function::
flash(u'Invalid password provided', 'error')
テンプレートの中でそのときはget_flashed_messages()
関数へカテゴリーも返すように伝える必要があります。そしてその状況ではループは少し違った見た目になります:
Inside the template you then have to tell the :func:`~flask.get_flashed_messages` function to also return the categories. The loop looks slightly different in that situation then:
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul class=flashes>
{% for category, message in messages %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
これは、どのようにフラッシュ表示されるメッセージを表示させるかについての単なる一つの例です。ある人は<strong>Error:</strong>
のような接頭辞をメッセージに加えるためにもカテゴリーを使うかもしれません。
This is just one example of how to render these flashed messages. One might also use the category to add a prefix such as ``<strong>Error:</strong>`` to the message.
フラッシュ表示するメッセージの選別(Filtering Flash Messages) Filtering Flash Messages¶
Changelog
バージョン 0.9 で追加.
オプションでget_flashed_messages()
の結果を選別(filter)するカテゴリーのリストを渡すことができます。これは各カテゴリーを分かれたブロックで表示したいときに便利です。
Optionally you can pass a list of categories which filters the results of :func:`~flask.get_flashed_messages`. This is useful if you wish to render each category in a separate block.
{% with errors = get_flashed_messages(category_filter=["error"]) %}
{% if errors %}
<div class="alert-message block-message error">
<a class="close" href="#">×</a>
<ul>
{%- for msg in errors %}
<li>{{ msg }}</li>
{% endfor -%}
</ul>
</div>
{% endif %}
{% endwith %}