Flask 消息閃現(xiàn)
w3cschool的教程代碼有好多錯(cuò)誤。這是運(yùn)行通過(guò)的。
一個(gè)好的基于GUI的應(yīng)用程序會(huì)向用戶提供有關(guān)交互的反饋。例如,桌面應(yīng)用程序使用對(duì)話框或消息框,JavaScript使用警報(bào)用于類似目的。
在Flask Web應(yīng)用程序中生成這樣的信息性消息很容易。Flask框架的閃現(xiàn)系統(tǒng)可以在一個(gè)視圖中創(chuàng)建消息,并在名為next的視圖函數(shù)中呈現(xiàn)它。
Flask模塊包含flash()方法。它將消息傳遞給下一個(gè)請(qǐng)求,該請(qǐng)求通常是一個(gè)模板。
flash(message, category)
其中:
- message參數(shù)是要閃現(xiàn)的實(shí)際消息。
- category參數(shù)是可選的。它可以是“error”,“info”或“warning”。
為了從會(huì)話中刪除消息,模板調(diào)用get_flashed_messages()。
get_flashed_messages(with_categories, category_filter)
兩個(gè)參數(shù)都是可選的。如果接收到的消息具有類別,則第一個(gè)參數(shù)是元組。第二個(gè)參數(shù)僅用于顯示特定消息。
以下閃現(xiàn)在模板中接收消息。
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
{% endwith %}
讓我們看一個(gè)簡(jiǎn)單的例子,演示Flask中的閃現(xiàn)機(jī)制。讓我們看一個(gè)簡(jiǎn)單的例子,演示Flask中的閃現(xiàn)機(jī)制。在以下代碼中,'/' URL顯示登錄頁(yè)面的鏈接,沒有消息閃現(xiàn)。
@app.route('/')
def flashindex():
return render_template('Flash.html')
該鏈接會(huì)將用戶引導(dǎo)到'/ login' URL,該URL顯示登錄表單。提交時(shí),login()視圖函數(shù)驗(yàn)證用戶名和密碼,并相應(yīng)閃現(xiàn)'success'消息或創(chuàng)建'error'變量。
@app.route('/login',methods = ['GET','POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid username or password. Please try again!'
else:
flash('You were successfully logged in!')
return redirect(url_for('flashindex'))
return render_template('FlashLogin.html',error = error)
如果出現(xiàn)錯(cuò)誤,則會(huì)重新顯示登錄模板,并顯示錯(cuò)誤消息。
FlashLogin.html
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{% if error %}
<p><strong>Error:</strong>{{ error }}
{% endif %}
<form action = "" method = "POST" >
<dl>
<dt>Username:</dt>
<dd><input type="text" name="username" value= "{{ request.form.username }}" /></dd>
<dt>Password:</dt>
<dd><input type="password" name="password" /></dd>
</dl>
<p><input type="submit" value="Login" /></p>
</form>
</p>
</body>
</html>
另一方面,如果登錄成功,則會(huì)在索引模板上刷新成功消息。
Flash.html
<html>
<head>
<title>Flash</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>Flask Message Flashing Example</h1>
<p>Do you want to <a href="{{ url_for('login') }}">
<b>log in ?</b> </a></p>
</body>
</html>
下面給出了Flask消息閃現(xiàn)示例的完整代碼:
Flash.py
from flask import Flask,flash,redirect,render_template,request,url_for,get_flashed_messages
app = Flask(__name__)
app.secret_key = 'random string'
@app.route('/')
def flashindex():
return render_template('Flash.html')
@app.route('/login',methods = ['GET','POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid username or password. Please try again!'
else:
flash('You were successfully logged in!')
return redirect(url_for('flashindex'))
return render_template('FlashLogin.html',error = error)
if __name__ == '__main__':
app.run()
執(zhí)行上述代碼后,您將看到如下所示的界面。

當(dāng)您點(diǎn)擊鏈接,您將被定向到登錄頁(yè)面。
輸入用戶名和密碼。

點(diǎn)擊登錄。如果登錄失敗將顯示“用戶名或密碼無(wú)效!請(qǐng)重試”

如果登錄成功,將顯示一條消息“您已成功登錄”。
