Django-Rest frameworw之分页

  • 时间:
  • 来源:互联网
  • 文章标签:

分页

============******============

                一、根据页码进行分页

                二、位置和个数进行分页

                三、游标分页

                四、luo的案例展示

============******============

分页示例展示

一、根据页码进行分页

in urls.py
	from django.conf.urls import url, include
	from rest_framework import routers
	from web.views import s9_pagination

	urlpatterns = [
	    url(r'^test/', s9_pagination.UserViewSet.as_view()),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework import serializers
	from .. import models

	from rest_framework.pagination import PageNumberPagination


	class StandardResultsSetPagination(PageNumberPagination):
	    # 默认每页显示的数据条数
	    page_size = 1
	    # 获取URL参数中设置的每页显示数据条数
	    page_size_query_param = 'page_size'

	    # 获取URL参数中传入的页码key
	    page_query_param = 'page'

	    # 最大支持的每页显示的数据条数
	    max_page_size = 1


	class UserSerializer(serializers.ModelSerializer):
	    class Meta:
	        model = models.UserInfo
	        fields = "__all__"


	class UserViewSet(APIView):
	    def get(self, request, *args, **kwargs):
	        user_list = models.UserInfo.objects.all().order_by('-id')

	        # 实例化分页对象,获取数据库中的分页数据
	        paginator = StandardResultsSetPagination()
	        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

	        # 序列化对象
	        serializer = UserSerializer(page_user_list, many=True)

	        # 生成分页和数据
	        response = paginator.get_paginated_response(serializer.data)
	        return response


二、位置和个数进行分页

in urls.py
	from django.conf.urls import url, include
	from web.views import s9_pagination

	urlpatterns = [
	    url(r'^test/', s9_pagination.UserViewSet.as_view()),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework import serializers
	from .. import models

	from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination


	class StandardResultsSetPagination(LimitOffsetPagination):
	    # 默认每页显示的数据条数
	    default_limit = 10
	    # URL中传入的显示数据条数的参数
	    limit_query_param = 'limit'
	    # URL中传入的数据位置的参数
	    offset_query_param = 'offset'
	    # 最大每页显得条数
	    max_limit = None

	class UserSerializer(serializers.ModelSerializer):
	    class Meta:
	        model = models.UserInfo
	        fields = "__all__"


	class UserViewSet(APIView):
	    def get(self, request, *args, **kwargs):
	        user_list = models.UserInfo.objects.all().order_by('-id')

	        # 实例化分页对象,获取数据库中的分页数据
	        paginator = StandardResultsSetPagination()
	        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

	        # 序列化对象
	        serializer = UserSerializer(page_user_list, many=True)

	        # 生成分页和数据
	        response = paginator.get_paginated_response(serializer.data)
	        return response

三、游标分页

in urls.py
	from django.conf.urls import url, include
	from web.views import s9_pagination

	urlpatterns = [
	    url(r'^test/', s9_pagination.UserViewSet.as_view()),
	]
in views.py
	#!/usr/bin/env python
	# -*- coding:utf-8 -*-
	from rest_framework.views import APIView
	from rest_framework import serializers
	from .. import models

	from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination


	class StandardResultsSetPagination(CursorPagination):
	    # URL传入的游标参数
	    cursor_query_param = 'cursor'
	    # 默认每页显示的数据条数
	    page_size = 2
	    # URL传入的每页显示条数的参数
	    page_size_query_param = 'page_size'
	    # 每页显示数据最大条数
	    max_page_size = 1000

	    # 根据ID从大到小排列
	    ordering = "id"



	class UserSerializer(serializers.ModelSerializer):
	    class Meta:
	        model = models.UserInfo
	        fields = "__all__"


	class UserViewSet(APIView):
	    def get(self, request, *args, **kwargs):
	        user_list = models.UserInfo.objects.all().order_by('-id')

	        # 实例化分页对象,获取数据库中的分页数据
	        paginator = StandardResultsSetPagination()
	        page_user_list = paginator.paginate_queryset(user_list, self.request, view=self)

	        # 序列化对象
	        serializer = UserSerializer(page_user_list, many=True)

	        # 生成分页和数据
	        response = paginator.get_paginated_response(serializer.data)
	        return response

四、luo的案例展示

in urls.py
        from django.conf.urls import url, include
	from web.views import app_pagina

	urlpatterns = [
	    url(r'^index/',app_pagina.IndexView.as_view()),
	]
 
in views.py
	from rest_framework import views
	from rest_framework import serializers
	from rest_framework.response import Response
	from rest_framework.pagination import LimitOffsetPagination,PageNumberPagination,CursorPagination

	from app_pagination import models

	class IndexSerializer(serializers.Serializer):
	    id = serializers.IntegerField()
	    name = serializers.CharField(max_length=32)
	    pwd = serializers.CharField(max_length=54)




	class P1(LimitOffsetPagination):
	    '''
	    LimitOffsetPagination 根据url?offset=1&limit=2来显示数据
	    不包括上下页URL = p1.paginate_queryset()
	    包括上下页URL = p1.get_paginated_response()
	    下一页URL = p1.get_next_link()
	    上一页URL = p1.get_previous_link()

	    当有几百万条数据时,访问越后面的数据查询速度越慢
	    记录当前访问页的数据id
	    '''
	    # 默认每页显示的数据条数
	    default_limit = 10
	    # URL中传入的显示数据条数的参数
	    limit_query_param = 'limit'
	    # URL中传入的数据位置的参数
	    offset_query_param = 'offset'
	    # 最大每页显得条数
	    max_limit = 3

	class P2(PageNumberPagination):
	    '''
	    最多显示120页
	    http://127.0.0.1:8000/index/?page=3
	    http://127.0.0.1:8000/index/?size=5
	    http://127.0.0.1:8000/index/?size=1&page=6  # 每页最多 page_size=2 个,如果组合使用 size不能超过page_size
	    '''

	    # 默认每页显示的数据条数
	    page_size = 1
	    # 获取URL参数中设置的每页显示数据条数
	    page_size_query_param = 'size'
	    # 获取URL参数中传入的页码key
	    page_query_param = 'page'
	    # 最大支持的每页显示的数据条数
	    max_page_size = 5


	class P3(CursorPagination):
	    '''
	    http://127.0.0.1:8000/index/?cursor=cD0y&page_size=1
	   对页码进行加密,只能点击上一页和下一页
	    bootstrap案例就是使用这样的方式
	    '''

	    # URL传入的游标参数
	    cursor_query_param = 'cursor'
	    # 默认每页显示的数据条数
	    page_size = 2
	    # URL传入的每页显示条数的参数
	    page_size_query_param = 'page_size'
	    # 每页显示数据最大条数
	    max_page_size = 1000

	    # 根据ID从大到小排列
	    ordering = 'id'

	class IndexView(views.APIView):
	    def get(self, request, *args, **kwargs):
	        user_list = models.UserInfo.objects.all()


	        #通过LimitOffsetPagination进行分页
	        # p1 = P1()
	        # page_user_list = p1.paginate_queryset(queryset=user_list, request=request, view=self)
	        # ser = IndexSerializer(instance=page_user_list, many=True)
	        # # return Response(ser.data)  # 不含上一页和下一页
	        # return p1.get_paginated_response(ser.data) # 含上一页和下一页

	        #通过PageNumberPagination进行分页
	        # p2 = P2()
	        # page_user_list = p2.paginate_queryset(queryset=user_list, request=request, view=self)
	        # ser = IndexSerializer(instance=page_user_list, many=True)
	        # # return Response(ser.data)  # 不含上一页和下一页
	        # return p2.get_paginated_response(ser.data) # 含上一页和下一页

	        # 通过CursorPagination进行分页
	        # 实例化分页对象,获取数据库中的分页数据
	        p3 = P3()
	        page_user_list = p3.paginate_queryset(queryset=user_list, request=request, view=self)

	        # 序列化对象
	        ser = IndexSerializer(instance=page_user_list, many=True)
	        # return Response(ser.data)  # 不含上一页和下一页
	        return p3.get_paginated_response(ser.data)  # 含上一页和下一页



本文链接http://www.taodudu.cc/news/show-647525.html