烧瓶路由模式匹配顺序 [英] Flask Route Pattern Matching Order

查看:98
本文介绍了烧瓶路由模式匹配顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 Flask Routes不是从上到下模式匹配,如何处理以下问题?



我有以下路线:

$ ol

  • / / close

  • /< poll_key> /< participant_key> ;

  • 如果我向 http:// localhost:5000 / example-poll-key / close ,Flask将它匹配为模式2,将字符串'close'指定给< participant_key> URL参数。如何在< participant_key> 路由之前使< poll_key> / close 解决方案

    > https://stackoverflow.com/a/17146563/880326 。



    看起来最好的解决方案是添加自己的转换器并创建路由为

      /< poll_key> / close 
    /< poll_key> /< no(close):participant_key>

    其中 no p>

      class NoConverter(BaseConverter):

    def __init __(self,map,* items):
    BaseConverter .__ init __(self,map)
    self.items = items
    $ b def to_python(self,value):
    self.items中的值:
    raise ValidationError()
    返回值






    <
    $ b

    我错过了 match_compare_key


    $ b $ true $,$($,$),$($,$,$) 1,200)])
  • 用于 /< poll_key> /关闭(True,-2,[(1,100),(0,-5)])

  • ; poll_key> /< participant_key> (True,-2,[(1,100),(1,100)]) / li>

    这意味着 static 具有比其他更高的优先级,靠近具有比< participant_key> 更高的优先级。



    示例:

      from flask import Flask 
    $ b $ app = Flask(__ name__)
    app.add_url_rule('/< poll_key> / close ','close',
    lambda ** kwargs:'close\t'+ str(kwargs))
    app.add_url_rule('/< poll_key> /< participant_key>','p_key ',
    lambda ** kwargs:'p_key''t'+ str(kwargs))


    client = app.test_client()

    打印client.get('/ example-poll-key / close')。data
    print client.get('/ example-poll-key / example-participant-key')。data
    <



    这个输出:关闭{'poll_key':u'example-poll-key'}
    p_key {'participant_key':u'example-participant-key','poll_key':u'example-poll-key'}

    看起来这是正确的行为。


    Given that Flask Routes are not pattern matched from top to bottom, how does one deal with the following problem?

    I have the following routes:

    1. /<poll_key>/close
    2. /<poll_key>/<participant_key>

    If I make a request to http://localhost:5000/example-poll-key/close, Flask matches it as pattern 2, assigning the string 'close' to the <participant_key> URL parameter. How can I make the <poll_key>/close route get matched before the <participant_key> route?

    解决方案

    See my other answer to the same question: https://stackoverflow.com/a/17146563/880326.

    Looks like the best solution is to add your own converters and create routes as

    /<poll_key>/close
    /<poll_key>/<no(close):participant_key>
    

    where the no converter is defined

    class NoConverter(BaseConverter):
    
        def __init__(self, map, *items):
            BaseConverter.__init__(self, map)
            self.items = items
    
        def to_python(self, value):
            if value in self.items:
                raise ValidationError()
            return value
    


    Update:

    I missed match_compare_key:

    1. for static endpoint: (True, -2, [(0, -6), (1, 200)])
    2. for /<poll_key>/close: (True, -2, [(1, 100), (0, -5)])
    3. for /<poll_key>/<participant_key>: (True, -2, [(1, 100), (1, 100)])

    This means that static has higher priority than the others and close has higher priority than <participant_key>.

    Example:

    from flask import Flask
    
    app = Flask(__name__)
    app.add_url_rule('/<poll_key>/close', 'close',
                     lambda **kwargs: 'close\t' + str(kwargs))
    app.add_url_rule('/<poll_key>/<participant_key>', 'p_key',
                     lambda **kwargs: 'p_key\t' + str(kwargs))
    
    
    client = app.test_client()
    
    print client.get('/example-poll-key/close').data
    print client.get('/example-poll-key/example-participant-key').data
    

    This outputs:

    close   {'poll_key': u'example-poll-key'}
    p_key   {'participant_key': u'example-participant-key', 'poll_key': u'example-poll-key'}
    

    Looks like this is the right behaviour.

    这篇关于烧瓶路由模式匹配顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆