views:

177

answers:

3

I'm trying to figure something out with the app.yaml file.

How do I get "nh" and "dover" out of my url: http://www.mysite.com/boats/nh/dover

I'm assuming would would do something like:

- url: /boats/<state>/<city>
  script: boats.py

and then be able to get the variables somehow but I've had a hard time finding documentation for this.

Thanks in advance

+1  A: 

They mention some stuff here, but its kind of vague:

http://code.google.com/appengine/docs/python/config/appconfig.html

+1  A: 
- url: /boats/.*
  script: boats.py

and you retrieve the rest of the URL through the request object.

class BoatsHandler( webapp.RequestHandler ):
  def get(self, url_fragment=None):
   #do something with url_fragment
jldupont
Ok, cool, how would I go about getting those variables out of said request object? (in boats.py I assume)
Travis
yes in boats.py
jldupont
This is incorrect - regexes in app.yaml are not passed to webapp apps.
Nick Johnson
It won't get the url fragment as an additional argument to the get function as you suggest in your code snippet.
Nick Johnson
Oh I see: I forgot to mention that the REGEXES must be setup when instantiating the webapp e.g. `application=webapp.WSGIApplication(URL_REGEXES)`
jldupont
That's kind of vital - and even then, regex components only get passed to the RequestHandler subclass if they're parenthesized, in which case you can just split out the components he originally asked for - see my answer.
Nick Johnson
+4  A: 

Regular expression groups in app.yaml don't get passed to webapps directly. But if you're using the webapp framework, groups in its routing regular expressions do get passed to webapps. For example:

app.yaml:

- url: /.*
  script: boats.py

boats.py:

class BoatsHandler(webapp.RequestHandler):
  def get(self, state, city):
    # Do something with state and city. They're strings, not ints, remember!

application = webapp.WSGIApplication([
    ('/boats/([^/]+)/([^/]+)', BoatsHandler),
])

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
Nick Johnson
@Nick: I have added comments to my answer: I just forgot to mention the setup of the webapp application with the regexes.
jldupont
Solid answer nick, great guidance dldupont, this is exactly what I was looking for, thank you.
Travis