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

Rau*_*ukh 3 python pagination mongodb pymongo flask

我通过以下代码获取 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,)
Run Code Online (Sandbox Code Playgroud)

我正在关注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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

Rau*_*ukh 5

已整理完毕,只需编辑

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,)
Run Code Online (Sandbox Code Playgroud)