在烧瓶网址中正则表达

时间:2016-07-12 07:14:59

标签: python regex flask flask-restful

我想用正则表达式在烧瓶中写一些网址。我有这样的django

r'^v1/$'
r'^v1/(.+/)$'

我想在烧瓶中重新创建它们

我试过以下

class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]
app.url_map.converters['regex'] = RegexConverter

api.add_resource(Handler1,'/<regex("^v1/$")>')
api.add_resource(Handler2,'/<regex("^v1/(.+/)$")>')

但它不起作用。 显示错误

ValueError: malformed url rule: '/<regex("^v1/$")>'

1 个答案:

答案 0 :(得分:1)

您未提供url_map规则值。它应该是:

class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]
app.url_map.converters['regex'] = RegexConverter

api.add_resource(Handler1,'/<regex("^v1/$"):just_v1>')
api.add_resource(Handler2,'/<regex("^v1/(.+/)$"):v1_plus>')

并且,在您的实际处理程序中(此处Handler1Handler2);您将接收这些映射值作为参数:

class Handler1(Resource):
    def get(self, just_v1):
        # you'll receive `v1/` as the value for `just_v1`