Flask中的CBV和正则表达式
一.CBV
def auth(func):
def inner(*args, **kwargs):
print("before")
result = func(*args, **kwargs)
print("after")
return result
return inner
class IndexView(views.View):
methods = ["GET"]
decorators = [auth, ]
def dispatch_request(self):
print("Index")
return "Index!"
#如果不传name,这所有返回的都是view,这样就会报错,所有人家必需你要传递参数
#然后他传递给view_func的其实就是你视图类中的dispatch_request方法。这样我们没有方法,在一个视图类中写多种要求方法
app.add_url_rule("/index", view_func=IndexView.as_view(name="index")) # name=endpoint
#或,通常常使用此方法
class IndexView(views.MethodView):
methods = ["GET"]
#cbv添加装潢,用这个,我们看as_view中就知道了
decorators = [auth, ]
def get(self):
return "Index.GET"
def post(self):
return "Index.POST"
#如果我们继承了MethodView,他帮我们重写了,dispatch_request方法,他给我们做了一个分发,通过要求,来履行不同的函数
app.add_url_rule("/index", view_func=IndexView.as_view(name="index")) # name=endpointdef auth(func):
def inn