读Flask:before(after)_request

Flask中可以使用@before_request等装饰器来注册一些函数,这些函数会在view_func之前被执行,通常可以用来做数据库初始化等操作。

1
2
3
4
5
6
7
8
9
10
def before_request(self, f):
"""Registers a function to run before each request."""
self.before_request_funcs.append(f)
return f

def preprocess_request(self):
for func in self.before_request_funcs:
rv = func()
if rv is not None:
return rv

Read More