gpt4 book ai didi

python - 对 python flask 中的项目列表进行分页

转载 作者:可可西里 更新时间:2023-11-01 10:00:34 27 4
gpt4 key购买 nike

我通过以下代码在 python 中获取项目列表:

from flask_paginate import Pagination
@app.route('/retrieve_data')
def retrieve():
PER_PAGE=5
connection = MongoClient()
db=connection.rheoML
fs = gridfs.GridFS(db)
search = False
q = request.args.get('q')
if q:
search = True
try:
page = int(request.args.get('page', 2))
except ValueError:
page = 1
List=fs.list()
pagination = Pagination(page=page,per_page=PER_PAGE, total=len(List), search=search, record_name='List')
return render_template("retrieveFile.html",List=List,fs=fs,form="submitIt",pagination=pagination,)

我正在学习 https://pythonhosted.org/Flask-paginate/ 上的教程此外,我在 retrieveFile.html 中执行以下操作以适应分页:

<form id="submitIt" name="submitIt" action="/GetFile" method="Post" onsubmit="return validate(this)">
{{ pagination.info }}
<table>
<tr>
<th>Select</th><th>Filename</th><th>Operator</th>
</tr>
{% for file in List %}
<tr>
<td>
<input type="checkbox" name="FileName" value={{file.strip('u').strip("'")}}><br>
</td>
<td>
<name="FileName1" id="file" value={{file.strip('u').strip("'")}}>{{file.strip('u').strip("'")}}<br>
</td>
<td>
{{fs.get_last_version(file).Operator}}<br>
</td>
</tr>
{% endfor %}
</table>
{{ pagination.links }}
<input type="submit" value="Download">
</form>

我将其添加到我的样式标签中

<style>
.pagination-page-info {
padding: .6em;
padding-left: 0;
width: 40em;
margin: .5em;
margin-left: 0;
font-size: 12px;
}
.pagination-page-info b {
color: black;
background: #6aa6ed;
padding-left: 2px;
padding: .1em .25em;
font-size: 150%;
}
</style>

问题是当页面显示时,页面编号没有问题,但所有项目都显示在一页本身中。请帮助我,我仍然是分页的初学者,仍在努力解决问题

最佳答案

整理好了只需要编辑

try:
page = int(request.args.get('page', 1))
except ValueError:
page = 1

List=fs.list()
i=(page-1)*PER_PAGE
List1=List[i:i+5]
pagination = Pagination(page=page,per_page=PER_PAGE, total=len(List), search=search, record_name='List')
return render_template("retrieveFile.html",List=List1,fs=fs,form="submitIt",pagination=pagination,)

关于python - 对 python flask 中的项目列表进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33556572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com