使用WSGIAppl和正则表达式分组帮助所需的Google App引擎Url映射

时间:2010-05-19 10:05:37

标签: google-app-engine url-mapping

从谷歌文档中获取此示例

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()

如何更改regx分组以便Product id是可选的

即,网址http://yourdomain.com/category将被发送到当前上述示例中的浏览处理程序,您必须添加产品ID或至少在类别之后添加

http://yourdomain.com/category/

R '/浏览/()/(。)'

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您可以使用映射到同一处理程序的两个正则表达式:

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()

答案 1 :(得分:-1)

尝试添加“?”在正则表达式结束之前:

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