cbv加装饰器
from flask import Flask
from flask.views import MethodView
app = Flask(__name__)
app.debug = True
### 登录认证--->不能公用--》要么只能给fbv用,要么只能给cbv用
def auth(func):
def inner(*args, **kwargs):
print(args) #cbv,会有第一个参数 self # 如果是fbv,就是空的
res = func(*args, **kwargs)
print('装饰器走了')
return res
return inner
@app.route('/', endpoint='index')
@auth
def index():
return 'hello flask'
class UserView(MethodView):
decorators = [auth] # 顺序从下往上
methods=['POST']
def get(self):
return 'user-get'
app.add_url_rule('/user', endpoint='user', view_func=UserView.as_view('user'))
if __name__ == '__main__':
app.run()