1. before_request
2. after_request
3. before_first_request
4. template_global
5. template_filter
6. errorhandler
from flask import Flask app = Flask(__name__) app.debug = True app.secret_key = 'flask' @app.before_request def before(): print('before request') @app.after_request def after(response): """ 装饰器after_request: 必须接收参数,而且还要有返回值 :param response: :return: """ print('after request') return response @app.route('/r') def test_before_after_request(): print('test_before_after_request') return 'test_before_after_request' if __name__ == '__main__': app.run()
before request
test_before_after_request
after request
如果定义多个before_request 装饰器,谁先定义,谁先执行
如果定义多个after_request 装饰器,谁先定义,谁后执行
before_first_request : 第一次请求才会执行,后面的请求都不会执行
@app.errorhandler(404) def error_handler(arg): print(arg) return '请求视图有误'