0

I'm looking for functionality similar to flask frameworks url_for for appengine. It takes a classes name redirects to url that's been associated with it in webapp2.WSGIApplication.

So if I have this.

app = webapp2.WSGIApplication([
  ("/", PostsPage),
  ("/login", LoginPage),
], debug=True)

So redirect(url_for(LoginPage)) would redirect me to /login.

tommis
  • 65
  • 5
  • possible duplicate of [webapp2 - How to reverse URL in templates?](http://stackoverflow.com/questions/8852737/webapp2-how-to-reverse-url-in-templates) – Lipis Jun 09 '14 at 11:00

2 Answers2

1

This is relatively easy to build if you have access to the argument that was passed to the WSGIApplication constructor:

class PostsPage:
  pass

class LoginPage:
  pass

urls = [
 ("/", PostsPage),
 ("/login", LoginPage),
]

def url_for(cls):
  return [x[0] for x in urls if x[1] == cls][0]

print url_for(LoginPage)
flamingcow
  • 341
  • 2
  • 7
1

You can name the different 'routes' like

  app = webapp2.WSGIApplication([
     webapp2.Route(r'/', handler=PostsPage, name ='main'),
     webapp2.Route(r'/login', handler=LoginPage, name ='login'),
  ], debug = True)

Now you can redirect using the name either as you suggest or using return self.redirect_to('login')or return self.redirect_to(main`)

I have included a return statement as the redirect first happens after you return.