Flask - 实现数据分页

本文介绍了使用Flask-SQLAlchemy的paginate方法进行数据分页,详细阐述了Pagination对象的属性和方法。同时,讨论了flask-paginate库以及Flask-Redis中数据分页的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、Flask-SQLAlchemy - 直接获取分页后的数据

1-0 基于 flsk-sqlalchemy 的批量数据插入 - add_all(list)

1-1 Pagination对象的常用属性

1-2 Pagination对象的常用方法

 二、flask-paginate - 对原始数据进行分页

三、Flask-Redis 的数据分页

3-1 简单指定当前页page和显示页数per_page获取分页数据


 

一、Flask-SQLAlchemy - 直接获取分页后的数据

flask-sqlalchemy 官方文档

Flask-SQLAlchemy 实现分页的简单学习

paginate(page=None, per_page=None, error_out=True, max_per_page=None)

Returns per_page items from page page.

If page or per_page are None, they will be retrieved from the request query. If max_per_page is specified, per_page will be limited to that value. If there is no request or they aren’t in the query, they default to 1 and 20 respectively.

如果page或per_page为None,则将从请求查询中检索它们。如果指定了max_per_page,则per_page将限制为该值。如果没有请求或它们不在查询中,则它们分别默认为1和20。

  • When error_out is True (default), the following rules will cause a 404 response(当error_out为True(默认值)时,以下规则将导致404响应):
  • No items are found and page is not 1. - 找不到任何项目且页面不是1.
  • page is less than 1, or per_page is negative. - page小于1,或per_page为负数。
  • page or per_page are not ints. - page或per_page不是整数。
  • When error_out is False, page and per_page default to 1 and 20 respectively.(当error_out为False时,page和per_page分别默认为1和20。)

Returns a Pagination object. - 返回 Pagination 对象

@app.route('/')
@login_required
def index():
    page=request.args.get('page',1,type=int)
        pagination=User.query.order_by(User.createTime.desc()).paginate(page,per_page=12,error_out=False)
    users=pagination.items
    return render_template('index.html', name=current_user.username,users=users,pagination=pagination)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值