从谷歌文档中获取此示例
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 '/浏览/()/(。)'
有什么想法吗?
答案 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/(.)/(.)?'