Search code examples
regexgoogle-app-enginepython-2.7webapp2

Regex in RedirectRoute not working correctly


I have looked into how to use a regex within a RedirectRoute using webapp2. I have tried formatting it the way it says to do so here: http://webapp-improved.appspot.com/guide/routing.html#the-url-template and I still cannot get my regex to work. Here is what I have:

RedirectRoute('/book/<bookId:([0-9]+)><title:(.*)>',handlers.book,name='book')

the call for this is:

self.redirect_to('book/%s_%s' %(bookId,title))

and an example of the url I am expecting is:

book/5907332378656768_The-Wizzard-of-Oz

When I run my app I get the following error:

Error: "Route named u'book/5907332378656768_The-Wizzard-of-Oz' is not defined.

Any help would be greatly appreciated.

EDIT:

Hey everyone! Thanks to Brent's response I was able to fix the problem. I had to change self.redirect_to('book/%s_%s' %(bookId,title)) to the following: self.redirect('book/%s_%s' %(bookId,title))

Thank you everyone for your help!


Solution

  • A Google search for webapp2 error route "is not defined" finds the Webapp2 source code. That error message is in the method default_builder() which is called from redirect_to(). That method looks for the name field ('book' in this case). Maybe you meant to call redirect() instead?