Django Rest Framework Pagination isn't working
My settings.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,
}
My views.py:
permission_classes = [IsAuthenticated]
queryset = Post.objects.all().order_by('-id')
serializer_class = PostSerializer
pagination_class = PageNumberPagination
The code should show 10 result in each page. But it's showing all the 13 result. The result is like this:
{
"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"
}
]
}
It should have two pages and in the first page it should show 10 posts list and on the other page it should show 3 posts. But it's not working like that. I tried others 'DEFAULT_PAGINATION_CLASS' but I got the same result. Why isn't it working?
#settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}
in views.py file
class SongViewSet(ModelViewSet):
with transaction.atomic():
queryset = Song.objects.all()
serializer_class = SongSerializer
http_method_names = ['post', 'get']
permission_classes = [IsAuthenticated]
pagination_class = PageNumberPagination
here you can see
