【问题标题】:List of 1000, create multiple pages with flask-paginate1000个列表,用flask-paginate创建多个页面
【发布时间】:2017-07-31 07:32:31
【问题描述】:

所以我有一个大约 1000 个项目的列表,并且想要使用分页创建多个页面,这样所有 1000 个项目就不会在一个页面中列出。我该怎么做呢?我目前正在使用flask作为前端框架并尝试使用flask-paginate,但它似乎不起作用。谁能给我指导?谢谢

{{pagination.info | safe}}

{% for user in users %}
<a href=/search/{{user}}>
<div class="panel panel-default col-md-6 col-md-offset-3">
    <div class="panel-body">
         {{user}}
        </div>
    </div>
</a>
{{pagination.links | safe}}
{% endfor%}

蟒蛇

> @app.route('/search/<int:page>')
> def search(page=1):
> page = request.args.get('page', type=int, default=1) 
> users = ['test1','test2', 'test3'] # Should be 1000 values or so 
> pagination = Pagination(page=page, total=len(users), search=True, record_name='users') 
> return render_template("search.html", users=users,
>                        search_query=search_query, pagination=pagination,)

【问题讨论】:

    标签: python html web flask pagination


    【解决方案1】:

    首先在路线中

    pagination = Pagination(page=page, total=len(users), search=True, record_name='users') 
    

    必须改正为

    pagination = Pagination(page=page, total=10, search=True, record_name='users')
    

    假设您希望每页 10 个项目。

    您可以查看此链接以更好地了解如何处理分页 http://www.ergo.io/blog/better-pagination-in-flask/

    【讨论】:

    • total 是项目总数,而不是每页的项目总数。要限制页面上的项目,只需再传递一个参数per_page=&lt;number_of_items_per_page&gt;
    猜你喜欢
    • 2016-08-18
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2014-07-20
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多