视图
FBV
from flask import Flask, session
app = Flask(__name__)
@app.route("/index")
def index():
return "ok"
if __name__ == '__main__':
app.run()
CBV
from flask import Flask, views
app = Flask(__name__)
def t1(func):
def inner(*args,**kwargs):
print("before1")
result = func(*args,**kwargs)
print("after1")
return result
return inner
def t2(func):
def inner(*args,**kwargs):
print("before2")
result = func(*args,**kwargs)
print("after2")
return result
return inner
class Index(views.MethodView):
methods = ["GET","POST"]
decorators = [t1,t2] # 执行额外的装饰器
def get(self):
print("get")
return "GET"
def post(self):
print("post")
return "POST"
app.add_url_rule("/index",view_func=Index.as_view("index"))
# 在CBV中不能使用装饰器形式的路由,只能使用上面这种;
# as_view("index")括号里的是别名;
if __name__ == '__main__':
app.run()