运维开发网

Django分页

运维开发网 https://www.qedev.com 2021-01-23 08:29 出处:51CTO 作者:Vaedit
方法1classTest(APIView):authentication_classes=[SessionAuthentication,JSONWebTokenAuthentication]permission_classes=[IsAuthenticated]defget(self,request,*args,**kwargs):result={"code":200,"message":"成功"

方法1

class Test(APIView):
    authentication_classes = [SessionAuthentication, JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        result = {
            "code": 200,
            "message": "成功",
            "data": ""
        }
        page = int(request.GET.get("page", 1))  # 页码
        limit = int(request.GET.get("limit", 10))  # 每页展示数量
        name = request.GET.get("name", "")  # 搜索
        if name == "":
            value_list = list(
                models.Test.objects.all().order_by("-id").values()[(page - 1) * 10: limit * page])
        else:
            value_list = list(
                models.Test.objects.filter(name__icontains=name).order_by("-id").values()[
                (page - 1) * 10: limit * page])

        res = {
            "list": value_list,
            "total": models.Test.objects.count()
        }
        result["data"] = res
        return JsonResponse(result)

<!--more-->

方法2

class TestViewSet(ModelViewSet):
    '''
        {
            "code": "200",
            "msg": "succss",
            "data": []
        }
    '''
    authentication_classes = [JSONWebTokenAuthentication]  # JWT验证
    permission_classes = [IsCasbinVerify]

    queryset = models.Test.objects.all()
    serializer_class = Test1Serializer

    def list(self, request, *args, **kwargs):
        res = {
            "code": 200,
            "msg": "success",
            "data": []
        }

        try:
            queryset = models.Test.objects.filter(name__icontains=request.GET.get("name", ""))

            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)

            serializer = self.get_serializer(queryset, many=True)
            res["data"] = serializer.data
        except Exception as e:
            res["code"] = -1
            res["msg"] = e.args[0]

        return JsonResponse(res)

方法3

class TestViewSet(ModelViewSet):
    authentication_classes = [JSONWebTokenAuthentication]  # JWT验证
    permission_classes = [IsCasbinVerify]
    queryset = models.Test.objects.all()
    serializer_class = TestSerializer

    def list(self, request, *args, **kwargs):
        res = {
            "code": 200,
            "msg": "success",
            "data": []
        }

        try:

            contact_list = list(models.Test.objects.filter(name__contains=request.GET.get("name", "")).order_by("-id").values("id","name"))
            paginator = Paginator(contact_list, 20)  # 默认每页显示20条,也可用前端传递size来自定义默认每页数据量

            page = request.GET.get('page')
            try:
                contacts = paginator.page(page)
            except PageNotAnInteger:
                # 如果请求的页数不是整数,返回第一页。
                contacts = paginator.page(1)
            except EmptyPage:
                # 如果请求的页数不在合法的页数范围内,返回结果的最后一页。
                contacts = paginator.page(paginator.num_pages)

            res["data"] = {
                "total": paginator.count,
                "list": contacts.object_list,
            }
            # return JsonResponse(res)
        except Exception as e:
            res["code"] = -1
            res["msg"] = e.args[0]
        return JsonResponse(res)
0

精彩评论

暂无评论...
验证码 换一张
取 消