Question

I am trying out webapp2 on app engine, and getting following error when I am trying to test it at route, http://localhost:8080/products/2. I should probably mention that the other 2 routes that you can see below ('/' & '/products') work just fine.

   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'),
])
Était-ce utile?

La solution

You've included a capture-group in your 'product' route, but haven't defined the corresponding keyword-argument in your handler function. Your handler class should look something like:

class MainPage(webapp2.RequestHandler):

def get(self, product_id=None):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello, World!')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top