04.PageNumberPagination分页

一.使用默认分页
1.settings 设置
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 2
}
2.参数
  • 1.第几页
# Client can control the page using this query parameter.
    page_query_param = 'page'
    page_query_description = _('A page number within the paginated result set.')
  • 2.每页大小
# 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.')
  • 3.最大页
# 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
二.自定义分页

avue 前端分页参数:

# 前端很喜欢用 data,把 result 改成 data
{ "total": 40, "pagerCount": 5, "currentPage": 1, "pageSize": 20, "pageSizes": [ 10, 20, 30, 40, 50, 100 ], "background": true }
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict, namedtuple

class LargeResultsSetPagination(PageNumberPagination):
    page_query_param = 'pagerCount'
    page_query_description = '当前第几页'
    page_size = 20
    page_size_query_param = 'pageSize'
    page_size_query_description = '每页条目数'
    max_page_size = 100

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('total', self.page.paginator.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('data', data)
        ]))

# settings
REST_FRAMEWORK = {
    # 指定用于支持coreapi的Schema
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
    'DEFAULT_PAGINATION_CLASS': 'utils.utils.LargeResultsSetPagination',
    'PAGE_SIZE': 10,
}

结果如下image

{
    "total": 104,
    "next": "http://127.0.0.1:8000/idcs/?pageSize=1&pagerCount=5",
    "previous": "http://127.0.0.1:8000/idcs/?pageSize=1&pagerCount=3",
    "data": [
        {
            "id": 4,
            "name": "华为机房0",
            "address": "神州路0号大院",
            "phone": "13412345678",
            "email": "mail_0@com.cn",
            "letter": "hw0"
        }
    ]
}
如果某个视图不需要分页,视图设置分页类为 None 即可
pagination_class = None
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jenvid.yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值