1. [
  1. 课程介绍
  2. ](_index_)
    1. [
  1. 引入DjangoRESTframework
  2. ](c01-introducetodrf_index)
  3. -
  4. [
  5. Web应用模式
  6. ](c01-introducetodrf_separatedfrontendandbackend)
    1. [
  1. 认识RESTful
  2. ](c01-introducetodrf_introducetorest)
    1. [
  1. RESTful设计方法
  2. ](c01-introducetodrf_howtodesignrest)
    1. [
  1. 使用Django开发REST接口
  2. ](c01-introducetodrf_developrestapiwithdjango)
    1. [
  1. 明确REST接口开发的核心任务
  2. ](c01-introducetodrf_coretasktodeveloprestapi)
    1. [
  1. DjangoRESTframework简介
  2. ](c01-introducetodrf_aboutdrf)
    1. [
  1. DRF工程搭建
  2. ](c02-drfproject_index)
  3. -
  4. [
  5. 环境安装与配置
  6. ](c02-drfproject_installandconfig)
    1. [
  1. 见识DRF的魅力
  2. ](c02-drfproject_thefirstdrfprogram)
    1. [
  1. Serializer序列化器
  2. ](c03-serializer_index)
  3. -
  4. [
  5. 定义Serializer
  6. ](c03-serializer_declaring)
    1. [
  1. 序列化使用
  2. ](c03-serializer_serializing)
    1. [
  1. 反序列化使用
  2. ](c03-serializer_deserializing)
    1. [
  1. 模型类序列化器ModelSerializer
  2. ](c03-serializer_modelserializer)
    1. [
  1. 视图
  2. ](c04-view_index)
  3. -
  4. [
  5. RequestResponse
  6. ](c04-view_requestandresponse)
    1. [
  1. 视图概览
  2. ](c04-view_view)
    1. [
  1. 视图说明
  2. ](c04-view_viewintroduction)
    1. [
  1. 视图集ViewSet
  2. ](c04-view_viewset)
    1. [
  1. 路由Router
  2. ](c04-view_routers)
    1. [
  1. 其他功能
  2. ](c05-components_index)
  3. -
  4. [
  5. 认证
  6. ](c05-components_authentication)
    1. [
  1. 权限
  2. ](c05-components_permissions)
    1. [
  1. 限流
  2. ](c05-components_throttling)
    1. [
  1. 过滤
  2. ](c05-components_filtering)
    1. [
  1. 排序
  2. ](c05-components_ordering)
    1. [
  1. 分页
  2. ](c05-components_pagination)
    1. [
  1. 版本
  2. ](c05-components_versioning)
    1. [
  1. 异常处理
  2. ](c05-components_exceptions)
    1. [
  1. 自动生成接口文档
  2. ](c05-components_documents)
    1. [
    2. Published with GitBook
    3. ](https://www.gitbook.com)

课程介绍

分页Pagination

REST framework提供了分页的支持。
我们可以在配置文件中设置全局的分页方式,如:

  1. REST_FRAMEWORK = {
  2. 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
  3. 'PAGE_SIZE': 100 # 每页数目
  4. }

也可通过自定义Pagination类,来为视图添加不同分页行为。在视图中通过pagination_clas属性来指明。

  1. class LargeResultsSetPagination(PageNumberPagination):
  2. page_size = 1000
  3. page_size_query_param = 'page_size'
  4. max_page_size = 10000
  1. class BookDetailView(RetrieveAPIView):
  2. queryset = BookInfo.objects.all()
  3. serializer_class = BookInfoSerializer
  4. pagination_class = LargeResultsSetPagination

注意:如果在视图内关闭分页功能,只需在视图内设置

  1. pagination_class = None

可选分页器

1) PageNumberPagination
前端访问网址形式:

  1. GET http://api.example.org/books/?page=4

可以在子类中定义的属性:

  • page_size 每页数目
  • page_query_param 前端发送的页数关键字名,默认为”page”
  • page_size_query_param 前端发送的每页数目关键字名,默认为None
  • max_page_size 前端最多能设置的每页数量 ``` from rest_framework.pagination import PageNumberPagination

class StandardPageNumberPagination(PageNumberPagination): page_size_query_param = ‘page_size’ max_page_size = 10

class BookListView(ListAPIView): queryset = BookInfo.objects.all().order_by(‘id’) serializer_class = BookInfoSerializer pagination_class = StandardPageNumberPagination

127.0.0.1/books/?page=1&page_size=2

  1. 2)**LimitOffsetPagination**<br />前端访问网址形式:

GET http://api.example.org/books/?limit=100&offset=400

  1. 可以在子类中定义的属性:
  2. - default_limit 默认限制,默认值与`PAGE_SIZE`设置一直
  3. - limit_query_param limit参数名,默认'limit'
  4. - offset_query_param offset参数名,默认'offset'
  5. - max_limit 最大limit限制,默认None

from rest_framework.pagination import LimitOffsetPagination

class BookListView(ListAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer pagination_class = LimitOffsetPagination

127.0.0.1:8000/books/?offset=3&limit=2

```