在路由中获取TypeError

时间:2013-12-31 17:43:05

标签: google-app-engine webapp2

我在app引擎上尝试使用webapp2,并在尝试在路由http://localhost:8080/products/2进行测试时遇到以下错误。我应该提一下,你可以在下面看到的其他两条路线('/'&'/ products')工作得很好。

   Traceback (most recent call last):
      File "/home/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
        rv = self.handle_exception(request, response, e)
      File "/home/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
        rv = self.router.dispatch(request, response)
      File "/home/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/home/harshal/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
        return handler.dispatch()
      File "/home/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/home/Downloads/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
        return method(*args, **kwargs)
    TypeError: get() got an unexpected keyword argument 'product_id'
    INFO     2013-12-31 17:36:55,084 module.py:617] default: "GET /products/2 HTTP/1.1" 500 228


class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')


application = webapp2.WSGIApplication([
    webapp2.Route(r'/', handler=MainPage, name='home'),
    webapp2.Route(r'/products', handler=MainPage, name='product-list'),
    webapp2.Route(r'/products/<product_id:\d+>', handler=MainPage, name='product'),
])

1 个答案:

答案 0 :(得分:4)

您已在“产品”路线中添加了捕获组,但未在处理函数中定义相应的keyword-argument。您的处理程序类应该类似于:

class MainPage(webapp2.RequestHandler):

def get(self, product_id=None):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello, World!')