views:

41

answers:

2

Hi

take this example from google docs

class BrowseHandler(webapp.RequestHandler):

>     def get(self, category, product_id):
>         # Display product with given ID in the given category.
> 
> 
> # Map URLs like /browse/(category)/(product_id) to
> BrowseHandler. application =
> webapp.WSGIApplication([(r'/browse/(.*)/(.*)',
> BrowseHandler)
>                                      ],
>                                      debug=True)
> 
> def main():
>     run_wsgi_app(application)
> 
> if __name__ == '__main__':
>     main()

How can i change the regx groupings so that Product id is optional

ie the url http://yourdomain.com/category will be sent to the browse handler in the current above example you must add a product id or at least the / after the category

ie

http://yourdomain.com/category/

r'/browse/(.)/(.)'

Any ideas?

A: 

Try to add a "?" before the end of your regexp:

r'/browse/(.)/(.)?'
Ilian Iliev
Isn`t this working or what?
Ilian Iliev
No this doesn't work throws an error in app engine python - also i need the /(.*) to be optional Jbochi solution will work as the 1st regx only needs one arg. Can this be done using one expression?
spidee
+2  A: 

You can use two regular expressions mapped to same handler:

class BrowseHandler(webapp.RequestHandler):

    def get(self, category, product_id=None):
        # Display product with given ID in the given category.


# Map URLs like /browse/(category)/(product_id) to
BrowseHandler. application =
webapp.WSGIApplication([(r'/browse/(.*)/(.*)', BrowseHandler),
                        (r'/browse/(.*)', BrowseHandler),
                       ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
jbochi
This solution works but the mappings need to be in reverse order - simple error.(r'/(.*)/(.*)', ns_cardHandler),(r'/(.*)', ns_cardHandler)works correctly!
spidee
You're right! I have edited the answer. Could use `$` too.
jbochi
what about if i wanted to pick up the mime type say path.html or path.rss or path.json so in my handler i could havedef get(self, category, product_id=None, mime):so a valid path might be tv/1010.htmlCan i do this is the regx and pass it into the handler?
spidee