一、简单使用
总路由
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]
应用下路由
from django.urls import path, re_path
from api import views
urlpatterns = [
path('books2/', views.BookView.as_view()), # 继承了ListAPIView
]
views.py
from api import models
from api.ser import BookModelSerializer
# 查所有,才需要分页
from rest_framework.generics import ListAPIView
# 内置三种分页方式
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination
'''
PageNumberPagination
page_size:每页显示的条数
'''
class BookView(ListAPIView):
queryset = models.Book.objects.all()
serializer_class = BookModelSerializer
# 配置分页
pagination_class = PageNumberPagination
settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1,
}
访问效果:
二、自定义分页器
views.py
from api import models
from api.ser import BookModelSerializer
# 查所有,才需要分页
from rest_framework.generics import ListAPIView
# 内置三种分页方式
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination
'''
PageNumberPagination
page_size:每页显示的条数
page_query_param:前端发送的页数关键字名称,(例如:page_query_param='page',查询时http://127.0.0.1:8000/api/books2/?page=2)
page_size_query_param='size':前端发送的每页数目关键字名,eg:查第一页展示6条 http://127.0.0.1:8000/api/books2/?page=1&size=6
max_page_size=5 # 设置前端最多能展示的每页数据数量
'''
class MyPageNumberPagination(PageNumberPagination): # 基本分页
page_size = 1
page_query_param = "aaa"
class BookView(ListAPIView):
queryset = models.Book.objects.all().filter(is_delete=False)
serializer_class = BookModelSerializer
# 配置分页
pagination_class = MyPageNumberPagination
效果:
补充:
# class MyLimitOffsetPagination(LimitOffsetPagination): # 偏移分页
# default_limit = 3 # 每页条数
# limit_query_param = 'limit' # 从标杆往后拿几条
# offset_query_param = 'offset' # 标杆
# max_limit = 5 # 每页最大几条
class MyCursorPagination(CursorPagination): # 游标分页,只能选择往前或者往后,不能直接跳到哪一页
cursor_query_param = 'cursor' # 每一页查询的key
page_size = 2 #每页显示的条数
ordering = '-id' #排序字段,-字段表示倒序
class BookView(ListAPIView):
# queryset = models.Book.objects.all().filter(is_delete=False)
queryset = models.Book.objects.all()
serializer_class = BookModelSerializer
#配置分页
pagination_class = MyCursorPagination
演示效果
标杆放在2的位置,从后取5个,就是3到7
游标分页,只能按给的上一页和下一页点,不能直接跳到哪一页
三、如果用APIView分页
from api import models
from api.ser import BookModelSerializer
# 查所有,才需要分页
from rest_framework.generics import ListAPIView
# 内置三种分页方式
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination
'''
PageNumberPagination
page_size:每页显示的条数
page_query_param:前端发送的页数关键字名称,(例如:page_query_param='page',查询时http://127.0.0.1:8000/api/books2/?page=2)
page_size_query_param='size':前端发送的每页数目关键字名
max_page_size=5 # 前端最多能设置的每页数量
'''
class MyPageNumberPagination(PageNumberPagination):
page_size = 1
page_query_param = "aaa"
# class BookView(ListAPIView):
# queryset = models.Book.objects.all().filter(is_delete=False)
# serializer_class = BookModelSerializer
# # 配置分页
# pagination_class = MyPageNumberPagination
# 如果使用APIView分页
from rest_framework.views import APIView
class BookView(APIView):
def get(self,request,*args,**kwargs):
book_list=models.Book.objects.all()
# 实例化得到一个分页器对象
page_cursor=MyPageNumberPagination()
book_list=page_cursor.paginate_queryset(book_list,request,view=self)
next_url =page_cursor.get_next_link() # 如果return 的是drf的Response,可以把下一页和上一页拼在Response中返回
pr_url=page_cursor.get_previous_link()
# print(next_url)
# print(pr_url)
book_ser=BookModelSerializer(book_list,many=True)
return page_cursor.get_paginated_response(data=book_ser.data)
效果:
文档更新时间: 2022-07-15 09:57 作者:李延召