3

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.

1
  • what groups of number are you referring to?? please reframe your question. Commented Mar 2, 2010 at 3:06

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So... you're suggesting that it's better to handle everything from one .py file, and parsing URL should be done there and passed to the appropriate functions, but instead of having a separate .py file for each different URL path?
You can have multiple handlers if you want, but most apps use a single handler for the bulk of the site. The handler scripts can import other modules to handle individual pages, if they wish.
You can have multiple .py files or a single. Doesn't really matter. You still have to have the url matchers in the handler (and app.yaml).
Missing quotes in both of your code samples are messing up code coloring. Tried to edit myself, but I have a six-character minimum for edits. =/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.