看到我对于同一个问题的其他答案:https://stackoverflow.com/a/17146563/880326。
看起来像最好的解决办法是增加你自己的转换器,并创建路由作为
/<poll_key>/close
/<poll_key>/<no(close):participant_key>
其中no
转换器定义
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
:
- 为
static
端点:(True, -2, [(0, -6), (1, 200)])
- 为
/<poll_key>/close
:(True, -2, [(1, 100), (0, -5)])
- 为
/<poll_key>/<participant_key>
:(True, -2, [(1, 100), (1, 100)])
这意味着static
比别人有更高的优先级和close
比<participant_key>
更高的优先级。
实施例:
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
此输出:
close {'poll_key': u'example-poll-key'}
p_key {'participant_key': u'example-participant-key', 'poll_key': u'example-poll-key'}
看起来这是正确的行为。
尝试在动态之前创建静态路由模式。看起来命令很重要。 – zhangyangyu