Passing a variable from one website to another on Tornado

328 views Asked by At

I would like to pass an object from one webpage ('localhost/form') to another redirect webpage ('localhost/redirect') using Tornado. My code snippets looks something like this..

class FormHandler (BaseHandler):
def get(self):
        redirect_page='localhost/redirect'
        some_variable='a variable that can only be generated in FormHandler'
        self.write('<button id="Redirect" type="button">Redirect</button><br><script> document.getElementById("Redirect").onclick = function () {location.href ="'redirect_page'";};</script>')


class RedirectHander (BaseHandler):
        self.write('The variable passed was'+some_variable)

def make_app():
return Application(
    [
        url('/', BaseHandler, { "var":"nothing" }, name="root"), # this is for the root! :)
        url('/form', FormHandler, { "var":"initialize this!" }, name = "forlorn"),
        url('/redirect', RedirectHandler, { "var":"initialize this!" }, name = "forlorn"),
    ],
    # settings:
    debug = True,
)
1

There are 1 answers

2
Michael Robellard On

Since a redirect is handled at the client side by the browser data passed during a redirect must be done in the url or by cookie.

Use Regular Expressions in your url to pass options in the url: http://www.tornadoweb.org/en/stable/web.html#tornado.web.URLSpec

Use RequestHandler.get_argument to get at Query String options

If you have a lot of data to pass between the two, first is this the right thing to do. second if it is then you can place the data somewhere safe on the server side (a database for example) and then in the redirect pass an id to find the record again.