Google App Engine: Give arguments to a script from URL handler?

google-app-engine, python

Solution

I see two questions, how to pass elements of the url path as variables in the handler, and how to get the catch-all to render properly.

Both of these have more to do with the main() method in the handler than the app.yaml

1) to pass the id in the `/detail/(\d)` url, you want something like this:

class DetailHandler(webapp.RequestHandler):
    def get(self, detail_id):
      # put your code here, detail_id contains the passed variable

def main():
  # Note the wildcard placeholder in the url matcher
  application = webapp.WSGIApplication([('/details/(.*)', DetailHandler)]
  wsgiref.handlers.CGIHandler().run(application)

2) to ensure your `Index.py` catches everything, you want something like this:

class IndexHandler(webapp.RequestHandler):
    def get(self):
      # put your handler code here

def main():
  # Note the wildcard without parens
  application = webapp.WSGIApplication([('/.*', IndexHandler)]
  wsgiref.handlers.CGIHandler().run(application)

Hope that helps.

Problem

Here is a portion of my `app.yaml` file: ``` handlers: - url: /remote_api script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py login: admin - url: /detail/(\d)+ script: Detail.py - url: /.* script: Index.py ``` I want that capture group (the one signified by `(\d)`) to be available to the script `Detail.py`. How can I do this? Do I need to figure out a way to access GET data from `Detail.py`? Also, when I navigate to a URL like `/fff`, which should match the `Index.py` handler, I just get a blank response.

Original source