如题 我是一个初学者 ,学校让写一个小项目 我选了图书管理系统。经过一些考虑之后 ,在网上看到了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>
请帮忙给出解决办法,我想要的是在查询之后获得到的数据,能在页面上原来显示所有数据的地方显示查询后的数据