我在我的应用程序中使用如下路线:
Route(r'/thing/<some_id>/foo', handler=ThingFoo, name="thing-foo")
这用于使用 生成一个 url uri_for('thing-foo', some_id="something")
,该 url 返回给他们存储的用户。然后,在另一个请求中,用户发布了之前返回给他们的 url,需要对其进行解析。
我想<some_id>
从提供的 url 中提取模式的值,其方式类似于路由器如何将值传递给 RequestHandler 的 get/post 方法,但似乎缺少这方面的文档。
有没有像下面这样的东西?
route, some_id = webapp2.extract_uri(the_url)
(当然我可以使用正则表达式直接提取值,但这似乎不是很干)。
这是我想做的一个例子。
def image_url(request, image_blob_key):
if image_blob_key:
return request.url_for('image', resource=image_blob_key, _full=True)
else:
return None
def blob_key_from_image_url(image_url):
# Do something here to calculate the blob_key from the URL.
return blob_key
在我的应用程序的一部分中,image_url 从 blob_key 计算并传递给用户。稍后,如果他们(例如)想要删除图像,他们将 url 在 POST 请求中传回,我想从中提取 blob_key 以便我可以删除它。