首页 > 其他分享 >falsk分页详细描述

falsk分页详细描述

时间:2023-09-05 23:36:03浏览次数:33  
标签:None 分页 items falsk 详细描述 per self query page

Flask-SQLAlchemy 也提供了一个 paginate()查询方法, 相关源码如下

def paginate(self, 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.

When ``error_out`` is ``True`` (default), the following rules will
cause a 404 response:

* No items are found and ``page`` is not 1.
* ``page`` is less than 1, or ``per_page`` is negative.
* ``page`` or ``per_page`` are not ints.

When ``error_out`` is ``False``, ``page`` and ``per_page`` default to
1 and 20 respectively.

Returns a :class:`Pagination` object.
"""

return Pagination(self, page, per_page, total, items)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
参数说明:
page: 指定页码,从1开始
per_page: 每一页显示几条数据
error_out:是否抛出错误(默认为True)大部分error_out 是False, page and per_page 默认值是 1和20
max_per_page:每页显示最大值当指定了max_per_page时,per_page会受到这个值的限制

调用 paginate()查询方法会返回一个Pagination 对象的实例

class Pagination(object):
"""Internal helper class returned by :meth:`BaseQuery.paginate`. You
can also construct it from any other SQLAlchemy query object if you are
working with other libraries. Additionally it is possible to pass `None`
as query object in which case the :meth:`prev` and :meth:`next` will
no longer work.
"""

def __init__(self, query, page, per_page, total, items):
#: the unlimited query object that was used to create this
#: pagination object.
self.query = query
#: the current page number (1 indexed)
self.page = page
#: the number of items to be displayed on a page.
self.per_page = per_page
#: the total number of items matching the query
self.total = total
#: the items for the current page
self.items = items
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Pagination类对象的属性主要有:
has_next:如果在目前页后至少还有一页的话,返回 True。
has_prev:如果在目前页之前至少还有一页的话,返回 True。
next_num:下一页的页面数。
prev_num:前一页的页面数。

另外还有如下的可调用方法:
iter_pages():一个迭代器,返回一个在分页导航中显示的页数列表。
prev():上一页的分页对象。
next():下一页的分页对象。

实例属性有
query:创建Pagination对象对应的query对象
total:匹配的元素总数
per_page:每一页显示的元素个数
items:当前页面的查询结果

分页查询接口
from flask import make_response, request
from http import HTTPStatus

@api.route('/demo')
class ProjectInfoView(Resource):


@api.doc(description='查询')
@api.marshal_with(project_model)
def get(self):
"""查询全部"""
api.logger.info(f"GET query查询参数: {request.args}")
# 按id倒序
objs = Demo.query.order_by(Demo.id.desc())
# 分页 page=None, per_page=None, error_out=True, max_per_page=None
page_objs = objs.paginate(
page=int(request.args.get("page", 1)),
per_page=int(request.args.get("size", 10)),
error_out=False,
max_per_page=50
).items
return page_objs, HTTPStatus.OK

标签:None,分页,items,falsk,详细描述,per,self,query,page
From: https://www.cnblogs.com/catfeel/p/17681156.html

相关文章

  • MySQL分页查询详解:优化大数据集的LIMIT和OFFSET
    最近在工作中,我们遇到了一个需求,甲方要求直接从数据库导出一个业务模块中所有使用中的工单信息。为了实现这一目标,我编写了一条SQL查询语句,并请求DBA协助导出数据。尽管工单数量并不多,只有3000多条,但每个工单都包含了大量的信息。DBA进行了多次导出操作,不幸的是,每次尝试导出都导致......
  • 分页查询
     //分页查询try{//第1步:获取第1页,每页5条数据intpageIndex=3;intpageSize=5;//第2步:获取总记录数,计算总页数longtotalNumber=db.Queryable<User>().Count();inttotalPa......
  • C#分页查询(AI)
    来自你的消息:C#完美的通用分页查询如何实现?请用接口定义并实现接口来自ChatAI的消息:要实现C#中的通用分页查询,可以使用接口来定义和实现接口。下面是一个示例代码:publicinterfaceIPagination<T>{IEnumerable<T>GetPage(intpageNumber,intpageSize);}publicc......
  • 【Qt初入江湖】Qt QSqlQueryModel 底层架构、原理详细描述
    鱼弦:全栈领域创作新星创作者、51CTO(Top红人+专家博主)、github开源爱好者(go-zero源码二次开发、游戏后端架构https://github.com/Peakchen) QtQSqlQueryModel是Qt中用于实现自定义SQL查询的模型类,它继承自QAbstractTableModel。QSqlQueryModel封装了对数据库查询结果的处理,可......
  • 【Qt初入江湖】Qt QSqlRelationalDelegate 底层架构、原理详细描述
    鱼弦:全栈领域创作新星创作者、51CTO(Top红人+专家博主)、github开源爱好者(go-zero源码二次开发、游戏后端架构https://github.com/Peakchen) Qt的QSqlRelationalDelegate类是用于在Qt模型/视图框架中使用带有外键关系的数据库表格数据的委托类。它提供了一组用于在表格视图......
  • Angular 实现分页器组件
    很感谢 angular实现简单的pagination分页组件-Amor丶Diamond-博客园(cnblogs.com) ,我根据这位博主代码做了修改,增加了跳转和每页行数功能.先看图:  //可配置项//totalItem数据总条数//maxSize:最多显示几页//pageSizes:行/页//moreBtn:是否显示......
  • 分页PageInterceptor
    依赖引入<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>xx.xx.xx</version></dependency>编码PageHelper.startPage()在查询前执行,设置Page放入ThreadLocal中/......
  • 关于分页查询数据重复的问题 (分页查看时数据库插入导致) 的一个解决办法
    既然是在我分页的过程中,插入了一条记录,导致我再查询下一页的时候,查出来上一页的记录,那么,我思考,我可不可以不去查询他新增的这一条记录。那么这样,就查询不到别人新增的这条记录了。那么需要的条件就出来了:数据库表要有创建时间的这么一个字段那么解决思路就出来了,首先是要前端进行......
  • thinkphp5 日期+group by + 分页paginate查询
    <?php//根据日期来统计所消耗的点数//模型的使用方法$result=$model->field("id,FROM_UNIXTIME(createtime,'%Y-%m-%d')as`createtime`,sum(price)ascount_price")->group("FROM_UNIXTIME(createtime,'%Y-%m-%d'),store......
  • mybatis plus 3.4以上分页无效问题,limit一直加不上,MybatisPlusInterceptor无效
    解决方案1、已注册@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();PaginationInnerInterceptorpaginationInnerInterceptor=newPaginationInnerInterceptor(D......