我通过以下代码在 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/