zoukankan      html  css  js  c++  java
  • DRF

    可以在 settings.py 中设置每页显示的数据数量

    REST_FRAMEWORK = {
        "PAGE_SIZE": 2,  # 每页显示 2 条内容
    }
    

    views.py:

    from rest_framework import serializers
    from rest_framework.pagination import CursorPagination
    from drf import models
    
    
    class MyPagination(CursorPagination):
        cursor_query_param = 'cursor'  # 参数
        page_size = 2  # 每页的数量
        ordering = 'id'  # 以 id 为排序数据
        page_size_query_param = None  # 每页数量的参数
        max_page_size = None  # 每页最大的数量
    
    
    class PagerSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.Role
            fields = "__all__"
    
    
    class PagerView(APIView):
        def get(self, request, *args, **kwargs):
            # 获取 Role 表中的所有数据
            role_obj = models.Role.objects.all()
            # 创建分页对象
            page_obj = MyPagination()
            # 将从数据库中获取的数据进行分页
            role_page = page_obj.paginate_queryset(
                queryset=role_obj,
                request=request,
                view=self,
            )
            # 对数据序列化
            ser = PagerSerializer(instance=role_page, many=True)
            # 使用分页的 response 进行返回
            return page_obj.get_paginated_response(ser.data)
    

    因为每一页的 URL 地址都是加密的,所以这里需要用分页的 response 进行返回,方便查看前一页和后一页的 URL 链接

    下一条数据的地址

    前后页面的地址都被加密了

  • 相关阅读:
    poj 2186 && hdu 3836
    poj 2833 The Average
    hud 3062 Party
    论 ACM 与泡妞 (转载)
    poj 1064 Cable master
    poj Candies
    [转]人才流失的背后
    获取存储过程的ReturnValue值
    javascript js jquery获取元素位置代码总结
    【引用】xmlpath 操作 xml
  • 原文地址:https://www.cnblogs.com/sch01ar/p/14303362.html
Copyright © 2011-2022 走看看