webapp2 redirect explained

robert king picture robert king · Sep 14, 2012 · Viewed 13.2k times · Source

I'm sometimes unsure how to use webapp2.redirect.

Is there ever a time when I should use self.redirect("/blah") instead of return self.redirect("/blah")

Here is my understanding/guess of the time-line: (sometimes i'm confused about if the response object does something or if webapp2 does it)

  1. I visit my multithreaded website www.mysite.com/name/robert, chrome sends a GET request (lets assume this request is a text file)
  2. webapp2 grabs this "text file" and turns it into a webapp2.Request. webapp2 also makes a new webapp2.Response.
  3. somehow the request url is given to the router for matching (either by webapp2 or by the response). An appropriate RequestHandler is instantiated. The RequestHandler's get() method is called with appropriate arguments.
  4. throughout this time there has only been one request and one response.
  5. the get() method calls response.out.write("hello world") adding "hello world" to the response body?
  6. the get() method calls self.redirect('/foo')
  7. Stuff happens
  8. the get() method calls self.out.write("bye world")
  9. the response is sent to the client containing hello world, what food added, bye world

example of the initial get function:

def get():
    self.write('hello world')
    self.redirect('/foo')
    self.write('bye world')

What "stuff happens"? I suppose the router finds /foo/'s RequestHandler. What modifications are made to the Request and Response before foo's requestHandlers get() method is called. Is the request deleted and replaced by a new GET request? Is the response deleted and replaced by a new one? What context remains that was present in the initial request handler? does code execution return to the initial request handlers get method and if so is the context that may have existed restored?

Sorry If this is a bit of a mouthful, I've tried explaining what I want to know :)

Perhaps it would have been easier to ask for some use cases (do's and don'ts) of using redirect instead.

Answer

lecstor picture lecstor · Sep 14, 2012

The redirect method is really just some useful fluff around setting the response status and response Location header. Nothing really happens until the response is sent to the client which follows the redirect. You would want to return the result of calling redirect simply to avoid more code being run if there was more after the redirect that you didn't want run.

The source is quite easy to read.. http://webapp2.readthedocs.io/en/latest/_modules/webapp2.html#redirect