python – Flask:如何在蓝图中的每个路径之前运行方法?
发布时间:2021-01-12 03:39:14 所属栏目:Python 来源:互联网
导读:我想让Flask Blueprint在执行任何路由之前始终运行一个方法.我没有用自定义装饰器装饰我的蓝图中的每个路线方法,而是希望能够做到这样的事情: def my_method(): do_stuffsection = Blueprint(section, __name__)# Register my_method() as a setup method th
我想让Flask Blueprint在执行任何路由之前始终运行一个方法.我没有用自定义装饰器装饰我的蓝图中的每个路线方法,而是希望能够做到这样的事情: def my_method(): do_stuff section = Blueprint('section',__name__) # Register my_method() as a setup method that runs before all routes section.custom_setup_method(my_method()) @section.route('/two') def route_one(): do_stuff @section.route('/one') def route_two(): do_stuff 然后基本上/ section / one和/ section / two将在执行route_one()或route_two()中的代码之前运行my_method(). 有没有办法做到这一点? 解决方法您可以使用 before_request装饰器来获取蓝图.像这样:@section.before_request def my_method(): do_stuff 这会自动注册要在属于蓝图的任何路由之前运行的函数. (编辑:百色站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 如何清理与send_file一起使用的临时文件?
- 想在Jupyter Notebook(Anaconda)中保存并运行Python脚本
- 使用Python在OpenOffice / Microsoft Word中格式化输出
- Python:ValueError和Exception之间的区别?
- Python ConfigParser检查Section和Key Value的存在
- 在cygwin下,如何配置Mercurial以使用WinMerge进行合并?
- 在python中生成没有闭包的函数
- python – pandas转义回车到to_csv
- 有没有办法在python中的特定索引附加/扩展列表与另一个列表
- Python:如何在字符串中剪切超过2个相等字符的序列