webapp2.Route与可选的领导部分 [英] webapp2.Route with optional leading part
问题描述
我正在学习 webapp2 框架及其强大的 Route 机制。
我的申请是应该接受这样的URI:
/ poll / abc-123
$ p
/ poll / abc-123 /
/ poll / abc-123 / vote /#post new
/ poll / abc-123 / vote / 456#view / update a vote
$ b民意调查可以选择性地组织成不同的类别,所以以上所有内容都应该也是这样的:
/ mycategory / poll / abc-123
/ mycategory / poll / abc-123 /
/ mycategory / poll / abc-123 / vote /
/ mycategory / poll / abc-123 / vote / 456
我的配置不正确:
app = webapp2.WSGIApplication([
webapp2.Route('/< category> / poll /< poll_id&<:/?> ',PollHandler),
webapp2.Route ('/< category> / poll /< poll_id> / vote /< vote_id>',VoteHandler),
],debug = True)
问题:如何解决我的配置问题?
针对GAE CPU时间/托管费进行优化。例如,如果我为每个条目添加两行,则速度可能会更快:一行包含类别,另一行包含不包含类别的内容... 解决方案
webapp2有一个重用常用前缀的机制,但在这种情况下,它们会有所不同,所以您不能避免重复这些路由,如下所示:
app = webapp2.WSGIApplication([
webapp2.Route('/ poll /< poll_id><:/?>',PollHandler),
webapp2.Route(' / poll /< poll_id> / vote /< vote_and>',VoteHandler),
webapp2.Route('/< category> / poll /< poll_id&<:/?>',PollHandler ),
webapp2.Route('/< category> / poll /< poll_id> / vote /< vote_and>',VoteHandler),
],debug = True)
您不必担心添加多条路线。它们的构建和搭配非常便宜。除非你有成千上万,否则减少路线数量并不重要。
小记:第一条路线接受可选结束斜线。如果使用 strict_slash = True
选项访问另一个,您可以改为使用 RedirectRoute
。这没有很好的记录,但已经有一段时间了。请参阅文档字符串中的说明。
I am learning the webapp2 framework with its powerful Route mechanism.
My application is supposed to accept URIs like these:
/poll/abc-123
/poll/abc-123/
/poll/abc-123/vote/ # post new vote
/poll/abc-123/vote/456 # view/update a vote
Polls may optionally be organized into categories, so all the above should work also like this:
/mycategory/poll/abc-123
/mycategory/poll/abc-123/
/mycategory/poll/abc-123/vote/
/mycategory/poll/abc-123/vote/456
My incorrect configuration:
app = webapp2.WSGIApplication([
webapp2.Route('/<category>/poll/<poll_id><:/?>', PollHandler),
webapp2.Route('/<category>/poll/<poll_id>/vote/<vote_id>', VoteHandler),
], debug=True)
Question: How could I fix my configuration?
If possible it should be optimized for GAE CPU-time/hosting fee. For example, it may be faster if I add two lines for each entry: one line with category and another one without category...
webapp2 has a mechanism to reuse common prefixes, but in this case they vary, so you can't avoid duplicating those routes, as in:
app = webapp2.WSGIApplication([
webapp2.Route('/poll/<poll_id><:/?>', PollHandler),
webapp2.Route('/poll/<poll_id>/vote/<vote_id>', VoteHandler),
webapp2.Route('/<category>/poll/<poll_id><:/?>', PollHandler),
webapp2.Route('/<category>/poll/<poll_id>/vote/<vote_id>', VoteHandler),
], debug=True)
You should not worry about adding many routes. They are really cheap to build and match. Unless you have tens of thousands, reducing the number of routes won't matter.
A small note: the first route accepts an optional end slash. You could instead use the RedirectRoute
to accept only one and redirect if the other is accessed, using the option strict_slash=True
. This is not well documented but has been around for a while. See the explanation in the docstring.
这篇关于webapp2.Route与可选的领导部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!