Optional URL Parameter in Route GAE webapp2

雨燕双飞 提交于 2019-12-06 04:22:49

问题


I'm really new to Python and GAE. I'm setting up a basic CRUD app for some test data and am trying to get some routing for the admin pages going. I'd like to use the same page for creating and editing an object. So basically I want:

/admin/edit/<id>

where <id> is optional and /admin/edit will route to the same page. I tried adding <id:\w*> to the route which then allowed me to hit the page without supplying an id, but then when I supplied the id, I received a 404. Then I tried <id:\w+> and got a 404 with and without an id. I'm not having much luck.

Can anyone help me with what regex I need for this?


回答1:


You can set up a regex to parse IDs out of the URL. Here's a really premitive example using webapp2:

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

And you setup your request handler to accept the additional parameter:

class PropertyHandler(webapp2.RequestHandler):
    def get(self, propertyId):

For a real-world implementation, you'd want to be a bit more specific on the regex and add validation to the handler incase you get garbage or no ID.



来源:https://stackoverflow.com/questions/11911354/optional-url-parameter-in-route-gae-webapp2

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