Flask框架學習筆記之消息提示與異常處理操作詳解
本文實例講述了Flask框架學習筆記之消息提示與異常處理操作。分享給大家供大家參考,具體如下:
flask通過flash方法來顯示提示消息:
from flask import Flask, flash, render_template, request, abort
app = Flask(__name__)
app.secret_key = '520'
@app.route('/')
def index():
flash("Hello loli")
return render_template("flash.html")
flash模板:flask開放了get_flashed_messages函數(shù)給模板使用,用來得到視圖函數(shù)中的flash里的字符串(消息)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello Login</h1>
<h2>{{ get_flashed_messages()[0] }}</h2>
</body>
</html>
這里制作一個簡單的表單模擬登陸界面提示:使用request方法得到輸入表單中的數(shù)據(jù)。
@app.route('/login', methods=['POST'])
def login():
# 獲取表單
form = request.form
# 獲取表單數(shù)據(jù)
username = form.get('username')
password = form.get('password')
# 若不存在username則flash(xxx)
if not username:
flash('Please input username')
return render_template("flash.html")
if not password:
flash('Please input password')
return render_template("flash.html")
if username == "loli" and password == "520":
flash("Login success")
return render_template("flash.html")
else:
flash("username or password wrong")
return render_template('flash.html')
表單模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello Login</h1>
<form action="/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit">
</form>
<h2>{{ get_flashed_messages()[0] }}</h2>
</body>
</html>

未輸入任何數(shù)據(jù)提示輸入username

未輸入密碼顯示的flash提示消息。

用戶名和密碼不符時。

登陸成功界面。
flask同樣可以自己設置404等錯誤界面:flask提供了errorhandler修飾器來設置自己的錯誤界面。
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")
自己設置的簡單404錯誤模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>404 頁面不存在</h1> <h2>Sorry</h2> </body> </html>

也可以在正常的界面發(fā)生404錯誤時轉到這個模板裝飾:用flask import abort方法來引起一個404錯誤. 只要user_id不為520則觸發(fā)404頁面。
@app.route('/users/<user_id>')
def users(user_id):
if int(user_id) == 520:
return render_template("user.html")
else:
abort(404)
user模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Loli </h1> </body> </html>
源碼:
#-*- coding:utf-8 -*-
from flask import Flask, flash, render_template, request, abort
app = Flask(__name__)
app.secret_key = '520'
@app.route('/')
def index():
flash("Hello loli")
return render_template("flash.html")
@app.route('/login', methods=['POST'])
def login():
# 獲取表單
form = request.form
# 獲取表單數(shù)據(jù)
username = form.get('username')
password = form.get('password')
# 若不存在username則flash(xxx)
if not username:
flash('Please input username')
return render_template("flash.html")
if not password:
flash('Please input password')
return render_template("flash.html")
if username == "loli" and password == "520":
flash("Login success")
return render_template("flash.html")
else:
flash("username or password wrong")
return render_template('flash.html')
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")
@app.route('/users/<user_id>')
def users(user_id):
if int(user_id) == 520:
return render_template("user.html")
else:
abort(404)
if __name__ == '__main__':
app.run()
希望本文所述對大家基于flask框架的Python程序設計有所幫助。
相關文章
python 讀取視頻,處理后,實時計算幀數(shù)fps的方法
今天小編就為大家分享一篇python 讀取視頻,處理后,實時計算幀數(shù)fps的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼
這篇文章主要介紹了python爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼,幫助大家更好的利用python 爬蟲爬取數(shù)據(jù),感興趣的朋友可以了解下2020-10-10
Jupyter?Notebook出現(xiàn)不是內部或外部的命令解決方案
這篇文章主要介紹了Jupyter?Notebook出現(xiàn)不是內部或外部的命令解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

