【问题标题】:How do I route specific paths using WSGIApplication()?如何使用 WSGIApplication() 路由特定路径?
【发布时间】:2010-12-21 20:26:23
【问题描述】:

目前我有 foo.com/bar 路由到请求处理程序 Main。我还希望 foo.com/bar/id 路由到该请求处理程序(其中“id”是对象的 id)。

这是我尝试过的,但失败了:

application = webapp.WSGIApplication(
                                     [('/bar', MainHandler),
                                     (r'/bar/(.*)', MainHandler)],
                                     debug=True)

我得到的错误是:

TypeError: get() takes exactly 1 argument (2 given)

【问题讨论】:

    标签: python google-app-engine web-applications httpwebrequest


    【解决方案1】:

    您需要更改MainHandler.get 方法的签名,如下所示:

    class MainHandler(webapp.RequestHandler):
        def get(self, bar_id=None):
            if bar_id is None:
                # Handle /bar requests
            else:
                # Handle /bar/whatever requests
    

    【讨论】:

    • 谢谢,这行得通。我真的不明白为什么在这种情况下我需要 var 。是因为我使用了正则表达式吗?
    • 通过使用该正则表达式,您是在告诉 AppEngine 您想要捕获 URL 的该部分并将其作为参数提供给您的 get() 处理程序。
    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多