This was posted to me
<*@google.com> wrote:
Hi Niklas, If you just want to map this: /region/city/category/ supposing its only this valid characters: [a-zA-Z0-9_] you can do the following: - main.py
application = webapp.WSGIApplication([('/([-\w]+)/([-\w]+)/([-\w]+)', handler)],debug=True)
and on your handler: class Handler(webapp.RequestHandler): def get(self, region, city, category): # Use those variables on the method Hope this helps!
My webapp is suppossed to handle URI like <region>/<city>/<category>
with optional city and category e.g.
/rio_de_janeiro/grande_rio_de_janeiro/casas? #<region>/<city>/<category>
/gujarat/ahmedabad/vehicles-for_sale #<region>/<city>/<category>
/rio_de_janeiro/grande_rio_de_janeiro/ #<region>/<city>
/delhi #<region>
Etc Now I want to enable a request handler that can take optional arguments divided by the separator /
. If I use the regex '/(.*)
and variables in the request handler the first varible becomes a/b
and the second variable becomes b
so that is nearly what I want but a and b as 2 different variables instead. The regex I tried for the request handler is
application = webapp.WSGIApplication([('/(.*)',MyPage),
And the function head of my request handler is
class MyPage(RequestHandler):
def get(self, location='frankfurt', category='electronics'):
To enable an HTTP query e.g. /frankfurt, /frankfurt/, /frankfurt/electronics, /madrid/apartments, /newyork, etc allowing all possible combinations. Can you advice me a regex that can achieve what I want? I want functionality like a mod_rewrite but for GAE.
Thanks
Clarification
It's just a question of "make the directory a variable" so to clarify here are some examples how it should behave
'/frankfurt', - put 'frankfurt' in variable 1 '/frankfurt/', - put 'frankfurt' in variable 1 '/frankfurt/electronics', - put 'frankfurt' in variable 1 and 'electronics' in virable 2 '/frankfurt/electronics/', same as above '/eu/frankfurt/electronics', same as above i.e. only last 2 groups count '/eu/frankfurt/electronics/', same as above 'toronto/lightnings', doesn't start with / so shan't work 'toronto/lightnings/', as above 'lima/cars/old', as above 'lima/cars/old/' as above
Typical cases I want to handle is /region/city/category i.e. if I apply the example to Brazil it could be /rio_de_janeiro/grande_rio_de_janeiro/casas? for /region/city/category or for India it could be /delhi/delhi/for_sale or /gujarat/ahmedabad/vehicles-for_sale
Solution
As far as I can tell the solution from the answer works for my purposes:
/(?:[^/]+)/?([^/]*)/?([^/]*)