分頁有三種方式html
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/
django
http://127.0.0.1:8000/api/page/?page=2&size=3
表示第二頁,每頁顯示三條數據
api
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/?
app
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/?
url
http://127.0.0.1:8000/api/page/?cursor=cD0z&size=3
這裏直接點擊下一頁url
並追加每頁顯示數據量參數size
spa
三種分頁中,普通分頁與django中的分頁基本沒有區別。不過要作分頁返回給前端數據,就要從數據庫中取出數據,而後在作分頁序列化。若是用戶一下在前幾頁請求數據庫中的最後幾頁數據,對查詢數據庫的時延,對數據庫的負載較大,就會出現問題,這個時候就可使用加密分頁,限制用戶的訪問,只提供前一頁和後一頁的接口。