是否定义了Python烧瓶的路径顺序?

时间:2014-07-29 09:10:23

标签: python python-2.7 flask

我的观点中有类似下面的设置:

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

在访问&#34; / test&#34时,似乎总是会调用示例中的函数测试。网址。这也是我想要的。但我在文档中找不到这种行为。这样定义的名称总是优先于变量吗?或者是重要的定义的顺序?我能否以任何方式确定优先级,以确保将来不会破坏?

1 个答案:

答案 0 :(得分:7)

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:
    """

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