webapp2 route fails

冷暖自知 提交于 2019-12-11 11:51:35

问题


I'm building my new website using app-engine with python and webapp2 I'm having hard times to define the URIs in my web application

the result I need is:

http://www.example.com/
http://www.example.com/products/
http://www.example.com/products/table

I thought it's an easy task, but apparently it is not (for me, anyway)

I'm getting 404 error when I'm trying to load something like that: http://www.example.com/products/chair/

where is my mistake?

app = webapp2.WSGIApplication([
webapp2.Route('/', MainPage),
webapp2.Route('/products/', handler=MainProductsHandler),
webapp2.Route('/products/(\w+)/', handler=ProductHandler)
],debug=True)

回答1:


OK, I solved it. just like that:

app = webapp2.WSGIApplication([('/', MainPage), ('/product/.*', MainPage)], debug=True)

I think that I had a problem when I used the webapp2.Route method

thanks anyway




回答2:


Your first approach would work using angle brackets wrapping the regular expresion like this:

app = webapp2.WSGIApplication([
webapp2.Route('/', MainPage),
webapp2.Route('/products/', handler=MainProductsHandler),
webapp2.Route('/products/<id:(\w+)>/', handler=ProductHandler)
],debug=True)

Don't forget to add the param id (or whatever name your choose for the regex match) to the handler's get method else it will complain about an unexpected param.



来源:https://stackoverflow.com/questions/8451914/webapp2-route-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!