本文讲述PageNumberPagination类的两种用法一、全局配置分页前期准备工作一个配置drf框架的django项目1. 在配置文件中添加下述代码代码解析指定全局分页器为PageNumberPagination且每页显示5条数据作用只要继承了 from rest_framework.generics 模块下的 GenericAPIView 类 无论直接间接自动获取分页能力# settings.py # REST Framework 配置 REST_FRAMEWORK { # 对所有分页器生效但优先级低 DEFAULT_PAGINATION_CLASS: rest_framework.pagination.PageNumberPagination, PAGE_SIZE: 5, # 每页显示5条数据 }2. 视图类代码如下这里以ListAPIView为例# views.py from rest_framework.generics import ListAPIView from .models import Product from .serializers import ProductSerializer class ProductListView(ListAPIView): queryset Product.objects.all() serializer_class ProductSerializer3. 路由类from django.urls import path from .views import ProductListView urlpatterns [ path(product/, ProductListView.as_view()), ]4. 基路由类from django.contrib import admin from django.urls import path, include urlpatterns [ path(admin/, admin.site.urls), path(api/, include(apps.filterapp.urls)) ]5. 接口工具测试使用接口工具测试下面的接口注意查询参数分别是 page、size 分别表示页码和数量localhost:8001/api/product/?page1size10响应结果解析count 25 表示数据库一共25条数据next http://localhost:8001/api/product/?page2size5 表示下一页的访问链接previous null 表示上一页的访问链接 但由于处于第一页没有上一页故为nullresult [ {}, {}, {}, {}, {} ] 表示数据库中具体的数据{ count: 25, next: http://localhost:8001/api/product/?page2size5, previous: null, results: [ { id: 1, name: 无穷辣条, price: 23.49, stock: 999, status: on_sale, created_at: 2026-06-05T00:00:00Z, category: 1 }, { id: 2, name: 卫龙大面筋, price: 15.99, stock: 500, status: on_sale, created_at: 2026-06-10T00:00:00Z, category: 1 }, { id: 3, name: 麻辣王子, price: 18.50, stock: 300, status: on_sale, created_at: 2026-06-08T00:00:00Z, category: 1 }, { id: 4, name: 亲嘴烧, price: 12.80, stock: 800, status: on_sale, created_at: 2026-06-12T00:00:00Z, category: 1 }, { id: 5, name: 魔芋爽, price: 25.00, stock: 450, status: on_sale, created_at: 2026-06-15T00:00:00Z, category: 1 } ] }二、具体视图类中自定义分页器先将全局配置文件中的drf关于分页的配置删去1.新增pagination.py文件自定义分页器其实就是继承PageNumberPagination类并定义下面四个属性from rest_framework.pagination import PageNumberPagination class ProductListPageNumberPagination(PageNumberPagination): 产品信息列表分页器 page_size 5 # 默认每页显示 5 条数据 max_page_size 20 # 每页最多允许显示 20 条即使客户端请求更大 page_size_query_param size # 客户端可通过 ?size10 动态指定每页数量 page_query_param page # 客户端通过 ?page2 请求第 2 页2.视图类视图类中指定自定义分页类from rest_framework.generics import ListAPIView from .models import Product from .serializers import ProductSerializer from .paginations import ProductListPageNumberPagination class ProductListView(ListAPIView): queryset Product.objects.all() serializer_class ProductSerializer pagination_class ProductListPageNumberPagination3. 接口工具测试localhost:8001/api/product/?page1size6{ count: 25, next: http://localhost:8001/api/product/?page2size6, previous: null, results: [ { id: 1, name: 无穷辣条, price: 23.49, stock: 999, status: on_sale, created_at: 2026-06-05T00:00:00Z, category: 1 }, { id: 2, name: 卫龙大面筋, price: 15.99, stock: 500, status: on_sale, created_at: 2026-06-10T00:00:00Z, category: 1 }, { id: 3, name: 麻辣王子, price: 18.50, stock: 300, status: on_sale, created_at: 2026-06-08T00:00:00Z, category: 1 }, { id: 4, name: 亲嘴烧, price: 12.80, stock: 800, status: on_sale, created_at: 2026-06-12T00:00:00Z, category: 1 }, { id: 5, name: 魔芋爽, price: 25.00, stock: 450, status: on_sale, created_at: 2026-06-15T00:00:00Z, category: 1 }, { id: 6, name: 可口可乐, price: 3.40, stock: 1999, status: draft, created_at: 2026-04-04T00:00:00Z, category: 2 } ] }三、总结虽然本文只讲了两种方式使用分页器但是也要变通我们自定义的分页器也可以作为全局的分页器进行使用。更多高级用法其实本质就是去重写分页器类的方法加入我们自己的逻辑。