简体   繁体   中英

Passing a variable from one website to another on Tornado

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,
)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM