【问题标题】:Passing a variable from one website to another on Tornado在 Tornado 上将变量从一个网站传递到另一个网站
【发布时间】:2015-08-24 13:54:30
【问题描述】:

我想使用 Tornado 将对象从一个网页('localhost/form')传递到另一个重定向网页('localhost/redirect')。 我的代码 sn-ps 看起来像这样..

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

【问题讨论】:

  • 问题到底是什么?你有错误吗?
  • 如何将变量从 localhost/form 传递到 localhost/redirect?
  • 没有错误。就是不知道怎么弄..

标签: python localhost tornado argument-passing


【解决方案1】:

由于重定向是在客户端由浏览器处理的,因此在重定向期间传递的数据必须在 url 或 cookie 中完成。

在你的 url 中使用正则表达式来传递 url 中的选项: http://www.tornadoweb.org/en/stable/web.html#tornado.web.URLSpec

使用RequestHandler.get_argument 获取查询字符串选项

如果您有大量数据要在两者之间传递,首先这是正确的做法。其次,如果是,那么您可以将数据放在服务器端的安全位置(例如数据库),然后在重定向中传递一个 id 以再次查找记录。

【讨论】:

  • 重定向处理程序会知道 some_variable 是什么吗?如何正确通过?
  • 比如说我想将一个 python 对象从一个 url 传递到另一个我该怎么做?
猜你喜欢
  • 2019-11-11
  • 1970-01-01
  • 2011-04-10
  • 2015-01-07
  • 2020-10-18
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多