from flask import Flask, redirect, request, render_template, url_for, flash
app = Flask(__name__)
app.secret_key = '123asd'
'''
在flask 中想要显示消息 就使用flash 这个函数
flash 有来个参数
messages 输入消息的内容
category 消息的类别 例如 info error warning
`info` 是提示性消息
`error`错误消息
`warning` 警告消息
'''
'''
页面中显示消息
可以使用 get_flashed_messages(with_categories=False, category_filter=[])
with_categories 设置是否采用类别 ,默认是False
category_filter 设置消息类别过滤器, with_categories = True 才需要
'''
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
uid = request.form.get('uid')
password = request.form.get('password')
if uid == 'xiaomo' and password == '123':
flash('登录成功','info')
return redirect(url_for('success'))
else:
flash('登录失败','error')
# abort(401)
return render_template('login.html')
else:
return render_template('login.html')
@app.route('/success/')
def success():
return render_template('success.html')
if __name__ == '__main__':
app.run(debug=True)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
user -- {% block title %}
{% endblock %}
</title>
</head>
<body>
<!--显示消息-->
{% with messages= get_flashed_messages() %}
{% if messages%}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
{% endwith %}
{% with messages= get_flashed_messages(True, ['error', 'info']) %}
{% if messages%}
<ul>
{% for message in messages %}
{% if message[0] == 'info' %}
<li class="success">{{message[1]}}</li>
{% else %}
<li class="error">{{message[1]}}</li>
{% endif %}
{% endfor %}
</ul>
{% endif%}
{% endwith %}
<div>
{% block header %}
{% endblock %}
<hr>
</div>
<div>
{% block body %}
{% endblock %}
<hr>
</div>
<div>
2020 year <a href="">关于我们</a>
</div>
</body>
</html>
/templates/bash.html

{% extends 'bash.html' %}
{% block title %}
用户登录
{% endblock %}
{% block header %}
用户登录
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/login.css') }}">
{% endblock %}
{% block body %}
<form action="/login" method="POST">
<table>
<tbody>
<tr>
<td>用户:</td>
<td><input type="text" name="uid"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr align="center">
<td colspan="2">
<button type="submit">确定</button>
<button type="reset">取消</button>
</td>
</tr>
</tbody>
</table>
session中uid的数据: {{ session['uid'] }}
{{uid}}
{{password}}
</form>
{% endblock %}
templates/login.html

{% extends 'bash.html' %}
{% block title %}
首页
{% endblock %}
{% block header %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/login.css') }}">
{% endblock %}
{% block body %}
首页--
{% endblock %}
/template/success.html

.error {
color: red;
}
.success {
color: green;
}
/static/css/login.css