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

发布于 2024-09-01 18:11:17 字数 958 浏览 7 评论 0原文

以谷歌文档为例,

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 分组,以便产品 id 是可选的,

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

http://yourdomain.com/category/

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-09-08 18:11:17

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

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

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()
你是暖光i 2024-09-08 18:11:17

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

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

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

r'/browse/(.)/(.)?'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文