普通分页

普通分页类似于Django中的分页

源码

class PageNumberPagination(BasePagination):
"""
A simple page number based style that supports page numbers as
query parameters. For example: http://api.example.org/accounts/?page=4
http://api.example.org/accounts/?page=4&page_size=100
"""
# The default page size.
# Defaults to `None`, meaning pagination is disabled.
page_size = api_settings.PAGE_SIZE # DjangoPaginator就是Django中的Paginator类
django_paginator_class = DjangoPaginator # Client can control the page using this query parameter.
page_query_param = 'page'
page_query_description = _('A page number within the paginated result set.') # Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.') # Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None last_page_strings = ('last',) template = 'rest_framework/pagination/numbers.html' invalid_page_message = _('Invalid page.')

源码重要的四个参数详解

# page_size是APISettings中的默认配置,用户配置则使用用户配置,未配置则使用DEFAULTS配置,默认为None
page_size = api_settings.PAGE_SIZE
# ?之后的参数,如127.0.0.1/books/?page=3
page_query_param = 'page'
# 每页最多显示的条数,如:127.0.0.1/books/?page=3&size=5
page_size_query_param = 'size'
# 超出后每页的最多显示数
max_page_size = None

简单使用

REST_FRAMEWORK = {
# 每页的显示数量
'PAGE_SIZE': 2
}

settings.py

views.py文件

from rest_framework.pagination import PageNumberPagination

class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
page = PageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)

或者可以自定义类来修改数据属性

from rest_framework.pagination import PageNumberPagination

class MyPageNumberPagination(PageNumberPagination):
page_size = 3
page_query_param = 'test'
page_size_query_param = 'size'
max_page_size = 5 class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
page = MyPageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)

不继承修改属性

from rest_framework.pagination import PageNumberPagination

class Book(ViewSetMixin, APIView):
def get_all(self, request):
response = {'status': 100, 'msg': '查询成功'}
book_list = models.Book.objects.all()
# 实例化产生一个分页对象
# 不继承来修改对象的值
page = PageNumberPagination()
page.page_size = 2
page.page_query_param = 'bb'
# page = MyPageNumberPagination()
# 第一个参数:要分页的数据,第二个参数request对象,第三个参数,当前视图对象
page_list = page.paginate_queryset(book_list, request, self)
# 再序列化的时候,用分页之后的数据
ser = mySer.BookSerializer(instance=page_list, many=True)
response['data'] = ser.data
return Response(response)
# 会带着链接,和总共的条数(不建议用)
# return page.get_paginated_response(ser.data)
# return Response(ser.data)

View.py

偏移分页

源码

class LimitOffsetPagination(BasePagination):
"""
A limit/offset based style. For example: http://api.example.org/accounts/?limit=100
http://api.example.org/accounts/?offset=400&limit=100
"""
default_limit = api_settings.PAGE_SIZE
limit_query_param = 'limit'
limit_query_description = _('Number of results to return per page.')
offset_query_param = 'offset'
offset_query_description = _('The initial index from which to return the results.')
max_limit = None
template = 'rest_framework/pagination/numbers.html'

重要参数详解

# 默认显示数量
default_limit = api_settings.PAGE_SIZE
# 标杆值,从哪开始偏移
offset_query_param = 'offset'
# 根据标杆值的偏移量
limit_query_param = 'limit'
# 超出后限制最多的显示多少条
max_limit = None

其余用法跟普通分页相同,都是实例化对象,对象调用paginate_queryset(self, queryset, request, view=None)方法,参数解释(queryset:要分页的数据,request:request对象,view:当前视图对象)

加密分页

源码

class CursorPagination(BasePagination):
"""
The cursor pagination implementation is necessarily complex.
For an overview of the position/offset style we use, see this post:
https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api
"""
cursor_query_param = 'cursor'
cursor_query_description = _('The pagination cursor value.')
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
template = 'rest_framework/pagination/previous_and_next.html' # Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.') # Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None # The offset in the cursor is used in situations where we have a
# nearly-unique index. (Eg millisecond precision creation timestamps)
# We guard against malicious users attempting to cause expensive database
# queries, by having a hard cap on the maximum possible size of the offset.
offset_cutoff = 1000

重要参数解释

# 按哪个字段排序
ordering = 'id'
# ?后的参数
cursor_query_param = 'cursor'
# 每页的显示数量
page_size = api_settings.PAGE_SIZE
# 每页最多显示的条数
page_size_query_param = 'size'
# 最大显示数
max_page_size = None

最后返回时调用对象的get_paginated_response(self, data)方法,返回一个加密后的页数链接

restful中的分页的更多相关文章

  1. tp中使用分页技术

    1 public function showList() { $m_ld = D ( 'guangxi_ld' ); $page = I ( 'get.p', 1 ); // 在配置中获取分页值 $p ...

  2. Oracle中经典分页代码!

    在Oracle中因为没有top关键字,所以在sqlserver中的分页代码并不适用于Oracle,那么在Oracle中如何来实现分页呢? --查询所有数据 STUNO STUNAME STUAGE S ...

  3. 在yii中使用分页

    yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...

  4. [数据库]Oracle和mysql中的分页总结

    Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...

  5. LigerUi中的Grid中不分页显示(local)!

    LigerUi中的Grid中不分页显示! grid为local usePager: true,                         //是否分页

  6. mongo中的分页查询

    /** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @p ...

  7. springboot中使用分页,文件上传,jquery的具体步骤(持续更新)

    分页: pom.xml     加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...

  8. jdbcTemplate 后台接口中的分页

    Springboot+jdbcTemplate  对查询结果列表做分页, 之前开发的小项目,数据逐渐增多,每次返回所有的查询结果,耗费性能和时间 想到做分页. 于是从简单的分页做起. jdbcTemp ...

  9. ListView 中 的 分页

     Django Pagination 简单分页 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验.Django 内置的 Pagination 能够帮助我 ...

随机推荐

  1. AOSP android 源码下载

    (1)下载 repo 工具 mkdir ~/bin PATH=~/bin:$PATH curl https://storage.googleapis.com/git-repo-downloads/re ...

  2. [译]C#7 Pattern Matching

    原文 public struct Square { public double Side { get; } public Square(double side) { Side = side; } } ...

  3. ubuntu16.04安装nvidia驱动及CUDA+cudnn

    网上查了资料,装好了,参照以下 https://blog.csdn.net/zhang970187013/article/details/81012845 https://blog.csdn.net/ ...

  4. luogu P3297 [SDOI2013]逃考

    传送门 gugugu 首先每个人管理的区域是一个多边形,并且整个矩形是被这样的多边形填满的.现在的问题是求一条经过多边形最少的路径到达边界,这个可以最短路. 现在的问题是建图,显然我们应该给相邻的多边 ...

  5. 【JS】正则向前查找和向后查找

    正向查找:就是匹配前面或后面是什么内容的,所以分类是:正向前查找,正向后查找 负向查找:就是匹配前面或后面不是什么内容的,所以分类是:负向前查找,负向后查找   操作符 说明 描述 (?=exp) 正 ...

  6. CPU缓存一致性协议—MESI详解

    MESI(也称伊利诺斯协议)是一种广泛使用的支持写回策略的缓存一致性协议,该协议被应用在Intel奔腾系列的CPU中. MESI协议中的状态 CPU中每个缓存行使用的4种状态进行标记(使用额外的两位b ...

  7. 什么是webservice?

    webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互. 这里具体举个例子,比如在Windows Server服务器上有个C#.Net开发的应用A,在Linux上有 ...

  8. 《从Paxos到Zookeeper:分布式一致性原理与实践》第一章读书笔记

    第一章主要介绍了计算机系统从集中式向分布式系统演变过程中面临的挑战,并简要介绍了ACID.CAP和BASE等经典分布式理论,主要包含以下内容: 集中式的特点 分布式的特点 分布式环境的各种问题 ACI ...

  9. Core Mvc传值Query、Form、Cookies、Session、TempData、Cache

    1.传值方法 使用Request的方法(1-3): 1)Query:获取链接?后面的值 如:http://localhost:55842/Home/About?name=kxy public IAct ...

  10. tidb调研

    TiDB是新一代开源分布式 NewSQL 数据库,相比较于我们常见的数据库MySQL,TiDB具有水平伸缩.强一致性的分布式事务.基于 Raft 算法的多副本复制等特性.同时,TiDB兼容MySQL生 ...