weixin_43850201 2020-07-15 07:23 采纳率: 0%
浏览 185

tornado怎么才能一个页面模板render多次

如题 我是一个初学者 ,学校让写一个小项目 我选了图书管理系统。经过一些考虑之后 ,在网上看到了tornado。

其中有一个功能是可以利用书籍名字或者作者信息查询书籍信息。

然后这是我第一次进去页面的时候,调用render渲染了全部的书籍信息。

    def get(self):
        # 1.读取数据库数据
        # 2.将的读取到的数据传递给模板
        # 3.模板中渲染数据
        connection = pymysql.connect(host='localhost',
                                     port=3306,
                                     user='root',
                                     password='123456',
                                     db='books',
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor
                                     )
        try:
            with connection.cursor() as cursor:
                sql = "select * from books;"
                cursor.execute(sql)
                result = cursor.fetchall()
                print(result)
        finally:
            connection.close()
        self.render('index.html', show_list=result)

但是当我点击查询的时候 再次调用render的时候,页面就不会改变

    def put(self):
        name_para = self.get_argument('name')
        author_para = self.get_argument('author')

        sql = "select * from books "
        if name_para != "" or author_para != "":
            sql += 'where '
            if name_para != "":
                sql += 'name like "%' + name_para + '%" '
                if author_para != "":
                    sql += 'and author like "%' + author_para + '%"'
            elif author_para != "":
                sql += 'author like "%' + author_para + '%"'
        print(sql)
        connection = pymysql.connect(host='localhost',
                                     port=3306,
                                     user='root',
                                     password='123456',
                                     db='books',
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor
                                     )
        try:
            with connection.cursor() as cursor:
                cursor.execute(sql)
                result = cursor.fetchall()
                print(result)
        finally:
            connection.close()
        self.render('index.html', show_list=result)

但是数据都查到了

这是html中的模板

<tbody>
                {% for book in show_list %}
                    <tr>
                        <th scope="row">{{ book['id'] }}</th>
                        <td>《{{ book['name'] }}》</td>
                        <td>{{ book['author'] }}</td>
                        <td>{{ book['type'] }}</td>
                        <td>{{ book['press'] }}</td>
                        <td>{{ book['publishDate'] }}</td>
                        <td>{{ book['price'] }}</td>
                        <td>
                            <button type="button" class="btn btn-outline-info upd-btn"
                                    value="{{ book }}" data-toggle="modal" data-target="#exampleModal">
                                修改
                            </button>
                            <button type="button" class="btn btn-outline-danger del-btn" value="{{ book['id'] }}">
                                删除
                            </button>
                        </td>
                </tr>
                {% end %}
              </tbody>

请帮忙给出解决办法,我想要的是在查询之后获得到的数据,能在页面上原来显示所有数据的地方显示查询后的数据

  • 写回答

2条回答 默认 最新

  • dabocaiqq 2020-08-01 16:33
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办