Search code examples
pythonregexwebapp2

webapp2: regex in routing


I have such a problem: I want one method of the class to handle different URIs (for URI "/solution/add" and "solution/edit"). So I wrote such routing:

app = webapp2.WSGIApplication([webapp2.Route(r'/solutions/(add|edit)', handler='solution.SolutionPage:add_edit_solution'), ], debug=True)

And webapp2 gives 404 error. Could you please suggest the solution of this problem?

Ofcourse I can write different routes for every URI, but it's not so interesting.)


Solution

  • As the webapp2 docs indicate, you have to put the regex in angle brackets, with a colon separating name and expression. The name is optional, but

    everything outside of <> is not interpreted as a regular expression to be matched

    So something like this: '/blog/<:\d{4}>/<:\d{2}>' Or in your case, this:

    webapp2.Route(r'/solutions/<:(add|edit)>',
                  handler='solution.SolutionPage:add_edit_solution')