一、消息闪现实现流程
image.pngimage.png
二、目录、代码与示例
image.pngapp.py
from flask import Flask, render_template,flash,redirect,request
app = Flask(__name__)
#session的安全极致,使用flask时,需要设置该随机串
app.secret_key = "abcdef"
#用户登录之后,跳转到个人中心,在个人中心页面,展示一个提示:登录成功
@app.route('/login',methods = ['GET','POST'])
def login():
""" 用户登录 """
if request.method == 'POST':
print('处理了登录的逻辑')
flash('登录成功',"success")
flash('欢迎回来',"success")
flash('错误提示',"error")
return redirect('/mine')
return render_template('login.html')
@app.route('/mine')
def mine():
""" 个人中心 """
return render_template('mine.html')
if __name__ == '__main__':
app.run(debug=True)
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h3>用户登录</h3>
<form action="{{ url_for('login') }}" method="post">
<div>
<input type="text" name="username" value="admin">
</div>
<div>
<input type="password" name="password">
</div>
<div>
<button type="submit">登录</button>
</div>
</form>
</body>
</html>
mine.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>个人中心</title>
<style type="text/css">
.success {
color: #0f0;
}
.error {
color: #f00;
}
</style>
</head>
<body>
<h3>个人中心</h3>
{% for category,message in get_flashed_messages(with_categories=true,category_filter=['error']) %}
<p class = "{{category}}">
{{category}} - {{ message }}
</p>
{% endfor %}
</body>
</html>
image.png
image.png
网友评论