您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – Flask:如何在蓝图中的每个路径之前运行方法?

5b51 2022/1/14 8:22:46 python 字数 1315 阅读 566 来源 www.jb51.cc/python

我想让Flask Blueprint在执行任何路由之前始终运行一个方法.我没有用自定义装饰器装饰我的蓝图中的每个路线方法,而是希望能够做到这样的事情: def my_method(): do_stuff section = Blueprint('section', __name__) # Register my_method() as a setup method that runs

概述

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().

有没有办法做到这一点?

@section.before_request
def my_method():
    do_stuff

这会自动注册要在属于蓝图的任何路由之前运行的函数.

总结

以上是编程之家为你收集整理的python – Flask:如何在蓝图中的每个路径之前运行方法?全部内容,希望文章能够帮你解决python – Flask:如何在蓝图中的每个路径之前运行方法?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶