消息提示

from flask import Flask, render_template, flash, request

app = Flask(__name__)
app.secret_key = "123456"


@app.route("/")
def hello_world():
    flash("hello, mr bean")
    return render_template("index.html")


@app.route("/login", methods=["POST"])
def login():
    form = request.form
    username = form.get("username")
    password = form.get("password")
    if not username:
        flash("please input username")
        return render_template("index.html")
    if not password:
        flash("please input password")
        return render_template("index.html")
    if username == "mr bean" and password == "123456":
        flash("login success")
        return render_template("index.html")
    else:
        flash("username or password is wrong")
        return render_template("index.html")


if __name__ == '__main__':
    app.run(debug=True)



#index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎登陆</title>
</head>
<body>
    <h1>welcome</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>
原文地址:https://www.cnblogs.com/themost/p/8452789.html