1、before_request裝飾器和after_request裝飾器html
咱們如今有一個Flask程序其中有3個路由和視圖函數前端
from flask import Flask app = Flask(__name__) # type:Flask @app.route("/login") def login(): return "Login" @app.route("/index") def index(): return "Index" @app.route("/home") def home(): return "home" app.run("0.0.0.0", 5000)
若是咱們須要讓用戶登陸才能夠看到index和home怎麼辦就須要認證下也就用到了session,查看session中有沒有數據就能夠了因此咱們就要在請求index頁面和home頁面以前斷定是否登錄了flask
1.@app.before_request在請求進入視圖函數以前執行session
from flask import Flask from flask import request from flask import redirect from flask import session app = Flask(__name__) # type:Flask app.secret_key = "DragonFire" @app.before_request def is_login(): if request.path == "/login": return None if not session.get("user"): return redirect("/login") @app.route("/login") def login(): return "Login" @app.route("/index") def index(): return "Index" @app.route("/home") def home(): return "Login"
@app.before_request就是這樣一個裝飾器,他所裝飾的函數,都會在請求進入視圖函數以前執行app
2.after_request在響應返回客戶端以前執行,結束視圖函數以後函數
@app.after_request def foot_log(environ): if request.path != "/login": print("有客人訪問了",request.path) return environ
2、Flask中的另外一種添加設置訪問路徑的方法以及Flask中的CBVpost
app.add_url_rule("/路徑名",view_func=類名.as_view(name="login"))
1.Flask中的CBVui
好比咱們設置一個登錄url
from flask import views class Login(views.MethodView): methods = ["GET","POST"] def get(self): returen "這是get請求返回" def post(self): print("這是post提交") app.add_url_rule("/login",view_func=Login.as_view(name="login"))
3、WTForms三方組件小插件spa
咱們先要安裝WTForms組件,安裝方法相似於Flask
好比咱們用WTForms返回一個登錄頁面帶驗證的那種咱們應該怎麼寫呢??
1.新建一個py文件用來寫咱們要返回的登錄頁面的內容
app01.py文件內容
from wtforms import Form,validators from flask import Flask app = Flask(__name__) class Loginform(Form): username = simple.StringField( label="用戶名", validators=[ validators.DataRequired(message="用戶名不能爲空"), validators.length(max=10,message="用戶名長度不能超過10") ] ) password = simple.PasswordField( label="密碼", validators=[ validators.DataRequired(message="密碼不能爲空"), validators.length(min=6,message="密碼長度不能小於%(min)s"), validators.length(max=10,message="密碼長度不能大於%(max)s") ] ) button = simple.SubmitField( label="登錄" ) class Login(views.MethodView): methods = ["GET","POST"] def get(self): login_form = Loginform() return render_template("login.html",loginform = login_form)
#把login_form類丟給前端文件 def post(self): login_form_data = Loginform(request.form)print(request.form.get("username")) print(request.form.get("password"))return "登錄成功" if __name__ == '__main__': app.run(debug=True)
2.咱們的前端文件html
login.html 文件中的內容
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! --> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> </head> <body> <form action="" method="post" novalidate> <p> {{ loginform.username.label }} {{ loginform.username }} {{ loginform.username.errors.0 }} </p> <p> {{ loginform.password.label }} {{ loginform.password }} {{ loginform.password.errors.0 }} </p> {{ loginform.button }} </form> </body> </html>