flask學習:實現一個簡單的登入的邏輯處理
阿新 • • 發佈:2018-11-09
最近在學習flask框架,果然比javaweb簡單很多。
需求是實現一個簡單的登入的邏輯處理。有判空和判斷不一致的操作。
其中app.py內容如下:
from flask import Flask, render_template,request,flash app = Flask(__name__) app.secret_key = 'xck' ''' 目的:實現一個簡單的登入的邏輯處理 ''' @app.route('/', methods=['GET', 'POST']) def hello_world(): #1.判斷請求方式 if request.method=="POST": #2.獲取請求的引數 username = request.form.get('username') password = request.form.get('password') password2 = request.form.get('password2') if (username == '') or (password == '') or (password2 == ''): flash(u"輸入不能為空!") elif password != password2: flash(u"密碼不一致!") else: return "success!" 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</title> </head> <body> <form method="post"> <lable>使用者名稱</lable><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 message in get_flashed_messages() %} {{ message }} {% endfor %} </form> </body> </html>