1. 程式人生 > >python3之flask入門Demo

python3之flask入門Demo

本程式碼是flask入門篇,適合初學者,閱讀時間大概三分鐘 ,實現輸入固定賬號密碼跳轉到百度首頁,持續更新,需要請關注

HTML檔案放在templates目錄下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div align="center">
<h2>User Management</h2>
    {% if message %} {{message}} {% endif %}
    <form method="POST">

        <input type="text" name="username" placeholder="username">
        <br>
        <input type="password" name="password" placeholder="password">
        <br>
        <input type="submit" value="Submit">
        <input type="reset" value="reset">
    </form>
</div>

</body>
</html>
from flask import Flask,request,render_template,redirect

app = Flask(__name__)
#繫結訪問地址127.0.0.1:5000/user
@app.route("/user",methods=['GET','POST'])
def login():
    if request.method =='POST':
        username = request.form['username']
        password = request.form['password']
        if username =="user" and password=="password":
            return redirect("http://www.baidu.com")
        else:
            message = "Failed Login"
            return render_template('login.html',message=message)
    return render_template('login.html')

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

 以下為效果圖