加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Flask Route Pattern匹配顺序

发布时间:2020-12-16 21:48:55 所属栏目:Python 来源:网络整理
导读:鉴于Flask Routes are not pattern matched from top to bottom,如何处理以下问题? 我有以下路线: / / close / / 如果我向http:// localhost:5000 / example-poll-key / close发出请求,Flask将其匹配为模式2,将字符串close分配给 URL参数.如何在 之前使

鉴于Flask Routes are not pattern matched from top to bottom,如何处理以下问题?

我有以下路线:

> /< poll_key> / 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.

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

/

其中没有定义转换器

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

更新:

我错过了match_compare_key:

>对于静态端点:(真,-2,[(0,-6),(1,200)])
> for /< poll_key> / close

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读