Pickels
Pickels

Reputation: 34632

Google App Engine: Redirect to RequestHandler

I just started with Google App Engine using python and I was following a tutorial and writing my own little app to get familiar the webapp framework. Now I just noticed the tutorial does the following self.redirect('/'). So that got me wondering: is there a way to redirect to a handler instead of a hardcoded path? Thought that might be better so that you can change your urls without breaking your app.

Upvotes: 3

Views: 1171

Answers (3)

n_l
n_l

Reputation: 864

The accepted answer made me built my own little router before I realized that in webapp2 you can name your routes and then redirect using that name as described in webapp2 Uri routing

app = webapp2.WSGIApplication(
              routes=[webapp2.Route('/', handler='RootController', name='root')])

and then redirect to them in the RequestHandler

self.redirect_to('root')

if your path contains placeholders you can supply the values for the placeholders and the webapp2.Router will build the correct uri for you. Again take a look at webapp2 Uri routing for more detailed information.

Upvotes: 0

Drew Sears
Drew Sears

Reputation: 12838

This isn't a limitation of App Engine so much as the webapp framework. webapp is intended to be a lightweight framework, providing just the essentials.

If you want fancier redirect behavior, try Django.

Upvotes: 1

Blair Conrad
Blair Conrad

Reputation: 241734

One alternative would be to have a map of symbolic names to URLs, that way you could redirect to the mapped URL - you could then update your URLs with impunity.

Or if you'd rather just execute the code from another handler, I don't know why you couldn't just make a method call - worst case, you could extract a common method from the two handlers and call that.

Upvotes: 3

Related Questions