Python flask实现网站登陆功能

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post">
    <label>用户名:</label><input type="text" name="username"><br>
    <label>密码:</label><input type="password" name="password"><br>
    <label>确认密码:</label><input type="password" name="password2"><br>
    <input type="submit" value="提交"><br>
    {# 使用遍历获取闪现的消息  #}
    {% for get_flashed_message in get_flashed_messages() %}
        {{ get_flashed_message }}
    
    {% endfor %}
    
</form>
</body>
</html>

Python代码

from flask import Flask, render_template, request, flash

app = Flask(__name__)

app.secret_key = 'password'


@app.route('/', methods=["GET", "POST"])
def index():
    if request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")
        password2 = request.form.get("password2")

        if not all([username, password, password2]):
            flash(u"参数不完整")

        elif password != password2:
            flash(u"密码错误")
        else:
            return "success"

    return render_template("index.html")


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

 

点赞