eg:
# cbv:基于类的视图 MethodView 继承自 View
from flask import Flask,render_template
from flask.views import MethodView
app = Flask(__name__,template_folder='templates')
# 3 写个装饰器
def auth(func):
def inner(*args,**kwargs):
print('装饰器执行了')
ret = func(*args,**kwargs)
return ret
return inner
# 1 定义类
class Home(MethodView):
decorators = [auth] # 使用装饰器 decorators类似一个列表
# 4控制允许的请求方式
methods = ['GET'] # 只允许get请求
def get(self):
return render_template('home.html')
def post(self):
return 'cbv'
# 2 注册路由
app.add_url_rule('/home',view_func=Home.as_view('home'))
if __name__ == '__main__':
app.run(port=8080)
标签:__,return,flask,MethodView,cbv,def From: https://www.cnblogs.com/abc683871/p/17629905.html