开发者

Google App Engine: Redirect to RequestHandler

开发者 https://www.devze.com 2023-01-08 04:05 出处:网络
I just started with Google App Engine using python and I was foll开发者_JS百科owing a tutorial and writing my own little app to get familiar the webapp framework. Now I just noticed the tutorial does

I just started with Google App Engine using python and I was foll开发者_JS百科owing 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消