标签:man 传参 查看 pager 取出 自定义分页 ati img ons
分页有三种方式
from django.urls import include
from django.conf.urls import url
urlpatterns = [
url(r'^api/', include('api.urls', namespace='api') ),
]
from django.urls import include
from django.conf.urls import url
urlpatterns = [
url(r'^page/$', PageView.as_view()),
]
在不使用django rest framework
中的分页组件仍能够达到目的
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.response import Response
class PagerSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = "__all__"
class PageView(APIView):
def get(self, request , *args, **kwargs):
roles = Role.objects.get_queryset().order_by('id')
roles_ser = PagerSerializer(instance=roles, many=True)
return Response(roles_ser.data) # 只返回数据
返回结果
from rest_framework.pagination import PageNumberPagination
class MyPageNumberPagination(PageNumberPagination):
page_size = 2
max_page_size = 5
page_size_query_param = 'size'
page_query_param = 'page'
class PagerSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = "__all__"
class PageView(APIView):
def get(self, request , *args, **kwargs):
roles = Role.objects.get_queryset().order_by('id')
page = MyPageNumberPagination()
page_roles = page.paginate_queryset(queryset=roles, request=request, view=self)
roles_ser = PagerSerializer(instance=page_roles, many=True)
# return Response(roles_ser.data) # 只返回数据
return page.get_paginated_response(roles_ser.data) # 返回前后夜url
http://127.0.0.1:8000/api/page/
http://127.0.0.1:8000/api/page/?page=2&size=3
表示第二页,每页显示三条数据
from rest_framework.pagination import LimitOffsetPagination
class MyLimitOffsetPagination(LimitOffsetPagination):
default_limit = 2
limit_query_param = 'limit'
offset_query_param = 'offset'
max_limit = 5
class PagerSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = "__all__"
class PageView(APIView):
def get(self, request , *args, **kwargs):
roles = Role.objects.get_queryset().order_by('id')
page = MyLimitOffsetPagination()
page_roles = page.paginate_queryset(queryset=roles, request=request, view=self)
roles_ser = PagerSerializer(instance=page_roles, many=True)
# return Response(roles_ser.data) # 只返回数据
return page.get_paginated_response(roles_ser.data) # 返回前后夜url
http://127.0.0.1:8000/api/page/?
http://127.0.0.1:8000/api/page/?offset=2&limit=3
表示从数据库中的第二条数据开始查询三条数据
使用加密分页的原因:如果使用普通分页时,由于向用户提供了可选参数page
,用户可以直接跳到数据分页之后的任意页码。但是这样做的后果就是,数据库的负载变大,返回结果的效率缓慢。但是一旦使用加密之后,虽然提供可选参数cursor
,但是对页码进行加密,用户无法知道当前页,而是以上一页下一页的方式翻阅数据。有效避免了数据库的负荷。但是就需要向用户提供上一页下一页的url
。
from rest_framework.pagination import LimitOffsetPagination
class MyCursorPagination(CursorPagination):
cursor_query_param = 'cursor'
page_size = 2
ordering = 'id'
page_size_query_param = 'size'
max_page_size = 5
class PagerSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = "__all__"
class PageView(APIView):
def get(self, request , *args, **kwargs):
roles = Role.objects.get_queryset().order_by('id')
page = MyCursorPagination()
page_roles = page.paginate_queryset(queryset=roles, request=request, view=self)
roles_ser = PagerSerializer(instance=page_roles, many=True)
return page.get_paginated_response(roles_ser.data) # 返回前后夜url
return page.get_paginated_response(roles_ser.data)
做返回http://127.0.0.1:8000/api/page/?
http://127.0.0.1:8000/api/page/?cursor=cD0z&size=3
这里直接点击下一页url
并追加每页显示数据量参数size
三种分页中,普通分页与django中的分页基本没有区别。不过要做分页返回给前端数据,就要从数据库中取出数据,然后在做分页序列化。如果用户一下在前几页请求数据库中的最后几页数据,对查询数据库的时延,对数据库的负载较大,就会出现问题,这个时候就可以使用加密分页,限制用户的访问,只提供前一页和后一页的接口。
标签:man 传参 查看 pager 取出 自定义分页 ati img ons
原文地址:https://www.cnblogs.com/welan/p/10162386.html