浮躁是人生最大的敌人

分页封装

Posted on 2019-07-24 00:12  King'home  阅读(591)  评论(0编辑  收藏  举报

分页封装

封装:course/utils.py
from rest_framework.pagination import PageNumberPagination

class CoursePageNumberPagination(PageNumberPagination):
    """课程列表数据的分页器"""
    page_query_param = "page"  # 页码参数
    page_size_query_param = 'page_size' # 单页数据量
    page_size = 5                       # 默认的单页数据量,就是每一页显示5个课程信息
    max_page_size = 20                  # 允许客户端设置的单页数据量
视图:course/views.py
from .utils import CoursePageNumberPagination
class CourseAPIView(ListAPIView):
    """课程列表接口"""
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by("-orders")
    serializer_class = CourseSerializer
    pagination_class = CoursePageNumberPagination
接口
http://127.0.0.1:8000/course/

Copyright © 2024 King'home
Powered by .NET 9.0 on Kubernetes