Django Rest Framework 分页不起作用

问题描述 投票:0回答:1

我的设置.py:

    'DEFAULT_AUTHENTICATION_CLASSES': [
        # 'rest_framework.authentication.TokenAuthentication',
        # 'rest_framework.authentication.BasicAuthentication',
        # 'rest_framework.authentication.SessionAuthentication',
        'authentication.authentication.ExpiringTokenAuthentication'
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ],
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
}

我的观点.py:

    permission_classes = [IsAuthenticated]
    queryset = Post.objects.all().order_by('-id')
    serializer_class = PostSerializer
    pagination_class = PageNumberPagination

代码应在每页显示 10 个结果。但它显示了所有 13 个结果。结果是这样的:

{
    "count": 13,
    "next": null,
    "previous": null,
    "results": [
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_SJpZeBc.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_Fs2qSAY.webp",
                    "order": 2
                }
            ],
            "caption": "my name is ayon",
            "created_at": "2024-02-21T09:40:46.069468Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_AvXbLoq.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_lRnFcgd.webp",
                    "order": 2
                }
            ],
            "caption": "friends will be great",
            "created_at": "2024-02-21T09:40:40.146627Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_XcnIJ6o.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_MPj4huO.webp",
                    "order": 2
                }
            ],
            "caption": "friends",
            "created_at": "2024-02-21T09:40:36.631495Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_LdvliP2.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_nLRwhrS.webp",
                    "order": 2
                }
            ],
            "caption": "friends foreever",
            "created_at": "2024-02-21T09:40:33.934885Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_iFAk1XG.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_EEnnvOX.webp",
                    "order": 2
                }
            ],
            "caption": "friends foreever",
            "created_at": "2024-02-21T09:40:27.121978Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_IrIuNi9.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_KYxr57M.webp",
                    "order": 2
                }
            ],
            "caption": "great day with great friends",
            "created_at": "2024-02-21T09:40:19.571672Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_Alntkoh.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_s7VSzdv.webp",
                    "order": 2
                }
            ],
            "caption": "Post by ayon",
            "created_at": "2024-02-21T09:40:08.201897Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_7c0fvJi.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_dccnFnB.webp",
                    "order": 2
                }
            ],
            "caption": "This post is a great post",
            "created_at": "2024-02-21T09:39:52.549697Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_KAyrIQb.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_MDf8aeY.webp",
                    "order": 2
                }
            ],
            "caption": "This post is a great post",
            "created_at": "2024-02-21T09:39:51.089595Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_SiMfptp.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_q22FQIM.webp",
                    "order": 2
                }
            ],
            "caption": "This post is a great post",
            "created_at": "2024-02-21T09:39:49.349847Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_w1jaZW8.jpeg",
                    "order": 1
                },
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_m05uOsM.webp",
                    "order": 2
                }
            ],
            "caption": "This post is a great post",
            "created_at": "2024-02-21T07:01:10.248190Z"
        },
        {
            "author": 2,
            "images": [
                {
                    "image": "http://127.0.0.1:8000/post_pics/temp_XTfbWGo.webp",
                    "order": 1
                }
            ],
            "caption": "This is a demo post",
            "created_at": "2024-02-21T06:40:30.948517Z"
        },
        {
            "author": 1,
            "images": [],
            "caption": "Hablu der jonno programming",
            "created_at": "2024-02-13T02:52:01Z"
        }
    ]
}

它应该有两页,第一页应显示 10 个帖子列表,另一页应显示 3 个帖子。但它不是那样工作的。 我尝试了其他“DEFAULT_PAGINATION_CLASS”,但得到了相同的结果。为什么不起作用?

python django django-rest-framework django-views pagination
1个回答
0
投票

#settings.py

REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
        'PAGE_SIZE': 10,
    }

在views.py文件中

class SongViewSet(ModelViewSet):
    with transaction.atomic():
        queryset = Song.objects.all()
        serializer_class = SongSerializer
        http_method_names = ['post', 'get']
        permission_classes = [IsAuthenticated]
        pagination_class = PageNumberPagination

在这里你可以看到

© www.soinside.com 2019 - 2024. All rights reserved.