gpt4 book ai didi

google-app-engine - 带有可选前导部分的 webapp2.Route

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:22 25 4
gpt4 key购买 nike

我正在学习 webapp2 框架及其强大的 Route 机制。

我的应用程序应该接受如下 URI:

/poll/abc-123
/poll/abc-123/
/poll/abc-123/vote/ # post new vote
/poll/abc-123/vote/456 # view/update a vote

民意调查可以选择性地组织成类别,因此以上所有内容也应该像这样工作:

/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 有一种重用公共(public)前缀的机制,但在这种情况下它们会有所不同,因此您无法避免重复这些路由,如:

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)

您不必担心添加很多路由。它们的 build 和匹配真的很便宜。除非您有数万条路线,否则减少路线数量无关紧要。

小提示:第一个路由接受一个可选的结束斜杠。您可以改为使用 RedirectRoute 仅接受一个并在访问另一个时重定向,使用选项 strict_slash=True。这没有很好的记录,但已经存在了一段时间。查看explanation in the docstring .

关于google-app-engine - 带有可选前导部分的 webapp2.Route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7049871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com