Flask--登录验证(多个装饰器)
登录验证(多个装饰器)
from flask import Flask,url_for,session,render_template
import functools
app = Flask(__name__)
def auth(func):
@functools.wraps(func)
def inner(*args,**kwargs):
username = session.get('name')
if not username:
return redirect(url_for("login"))
return func(*args,**kwargs)
return inner
@app.route("/index",endpoint="homepage") # 别名
@auth # 登录验证放在下面
def index():
data_dict = DATA_DIC
return render_template("index.html",data_dict=data_dict)
# 由于Flask别名默认为函数名并且重复会导致报错,所以需要加上functools