【问题标题】:Tornado: display updated data in html table from mysql databaseTornado:在 mysql 数据库的 html 表中显示更新的数据
【发布时间】:2016-05-16 10:06:56
【问题描述】:

我正在努力设置一个龙卷风网络应用程序,以便每次将新记录插入 MySQL 表时,在刷新 html 页面(甚至更好地不刷新)后,渲染的 html 表中的数据都会更新。

请求处理程序代码如下:

class TestHandler(tornado.web.RequestHandler):  
    def get(self):  
        data=sess.query(Country).all()
        self.render("test.html", data=data)   

html表格代码如下:

 <table id="example"  >
    <thead>
        <tr>
            <th>name</th>
            <th>capital</th>
        </tr>
    </thead>
    <tbody>
        {% for dt in data%}
          <tr>
              <td>{{dt.name}}</td>
              <td>{{dt.capital}}</td>
          </tr> 
        {% end %}
    </tbody>
</table>

目前,对于 mysql 表端的任何更新,html 表都没有更新。只有重启tornado server时,才会出现新的数据。这个问题可能很初级,但我真的需要一些指导。

【问题讨论】:

    标签: html mysql html-table tornado


    【解决方案1】:

    问题解决了。由于我使用 SQLAlchemy 访问 MySQL 数据库,我需要结束原始会话并创建一个新会话。

    class TestHandler(tornado.web.RequestHandler):  
        def get(self):   
            sess = Session()
            data=sess.query(Country).all()
            sess.close()
            self.render("test.html", data=data) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2014-03-24
      • 2017-09-06
      相关资源
      最近更新 更多