from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response from collections import OrderedDict import math # 自定义分页 class MyPageNumberPagination(PageNumberPagination): # 1,设置页面大小 page_size_query_param = "page_size" # 2,设置页面最大值 max_page_size = 5 # 3,设置默认每页显示记录数 page_size = 6 def get_paginated_response(self, data): return Response(OrderedDict([ ('count', self.page.paginator.count), ('count_page', math.ceil( self.page.paginator.count/self.page_size )), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data) ]))