flask flash闪消息传到前端使用栗子

一、主要程序  app.py

from flask import Flask, render_template, request,flash
import pymysql
app = Flask(__name__)
app.secret_key='asdfads'   #消息传递需要加密
@app.route('/a12',methods=["GET","POST"])
def a12():
    if request.method=="POST":
        username=request.form.get ('username')
        password = request.form.get('password')
        password2 = request.form.get('password2')
        print([username,password,password2])
        if not all ([username,password,password2]):
            flash(u"用户名或密码不完整")
        elif password!=password2:
            flash(u"两次密码不一致")
        else:
            flash(u"密码和账号都输入正确,恭喜你!")
    return render_template("a12.html")
if __name__ == '__main__':
 app.run(host="0.0.0.0", port=5000, debug=True)

二、前端页面 '/templates/a12.html'

<html lang="cn-zh">
<body>
<form action="a12" method="post" >
    <table class="table table-bordered" align="center">
        <tr><td>用户名:</td><td><input type="text" name="username" value=""></td></tr>
        <tr><td>密码:</td><td><input type="password" name="password" value=""></td></tr>
        <tr><td>确认密码:</td><td><input type="password" name="password2" value=""></td></tr>
        <tr><td colspan="2"><input type="submit" value="submit"  align="right"></td></tr>
        <tr>
            <td colspan="2">
                {% for message in get_flashed_messages() %}
                    <font color="blue" >{{message }}</font>
                {% endfor %}
            </td>
        </tr>
    </table>
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值