gpt4 book ai didi

python - 是否定义了 Python flask 的路由顺序?

转载 作者:行者123 更新时间:2023-11-28 20:44:56 31 4
gpt4 key购买 nike

在我看来,我有一个类似于以下的设置:

@app.route("/test")
def test():
...
@app.route("/<to>")
def page(to):
...

似乎在访问“/test” url 时总是会调用示例中的函数测试。这也是我想要的。但是我在文档中找不到这种行为。是不是定义的名称总是优先于变量?还是重要的是定义的顺序?我能否以任何方式设置优先级以确保这在未来不会中断?

最佳答案

Flask 使用 Werkzeug处理路由,并根据路由中可变部分的数量对路由进行排序。

/test没有可变部分,而 /<to> ,所以它会尝试匹配 /test首先。

目前,排序是根据 Rule.match_compare_key() function 完成的,记录为:

def match_compare_key(self):
"""The match compare key for sorting.

Current implementation:

1. rules without any arguments come first for performance
reasons only as we expect them to match faster and some
common ones usually don't have any arguments (index pages etc.)
2. The more complex rules come first so the second argument is the
negative length of the number of weights.
3. lastly we order by the actual weights.

:internal:
"""

权重由静态路径部分决定(比动态部分权重更大,首先匹配较短的路径),或由转换器特定权重决定(数字转换器在基于字符串的转换器之前排序,在任意类型之前排序路径转换器)。

关于python - 是否定义了 Python flask 的路由顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25011782/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com