Flask特殊装饰器-@app.before_request、@app.after_request、@app.errorhandler
#Flask特殊装饰器 #@app.before_request #请求进入视图函数之前 #@app.after_request #请求结束返回响应客户端之前 #正常 be1-be2-be3 -vf -af3-af2-af1 #异常 be1-be2-af3-af2-af1 #重定义错误信息 #@app.errorhandler(4XX or 5XX) 错误处理,(4XX or 5XX)代表响应码 from flask import Flask,request,session,redirect,render_template,send_file app = Flask(__name__) app.secret_key='@HID*@#' # # def warp(fun): # def inner(*args,**kwargs): # # if session.get("user"): # ret = fun(*args,**kwargs) # else: # return redirect("/login_se") # print("校验完成") # # return ret # return inner @app.before_request def be1(): print("我是be1") @app.before_request def be2(): print("我是be2") if request.path == "/login_se": return None if session.get("user"): return None else: return redirect("/login_se") @app.before_request def be3(): print("我是be3") @app.after_request def af1(res): print("af1") return res @app.after_request def af2(res): print("af2") return res @app.after_request def af3(res): print("af3") return res @app.route("/login_se",methods=["GET","POST"]) def login_se(): if request.method == 'GET': return render_template("login_new.html") else: session["user"]=request.form.get("username") return "登陆成功" @app.route("/index") def index(): print("我是视图函数") return render_template("home.html") @app.route("/index1") def index1(): return render_template("home.html") @app.route("/index2") def index2(): return render_template("home.html") @app.errorhandler(404) def error404(ErrorMessgae): print(ErrorMessgae) # return f"你访问的{request.path}不存在" # return redirect("/login_se") return send_file("my_picture/111.png") if __name__ == '__main__': app.run()