마크다운 토글코드
<details>
<summary>TITLE</summary>
<div markdown="1">
CONTENT
</div>
<details>
🔥구현 목표
💡아! 페이지네이션은 이렇게 요청하고, 데이터를 반환하는구나!
# Request
GET https://api.example.org/accounts/?page=4
# Response
HTTP 200 OK
{
"count": 1023,
"next": "https://api.example.org/accounts/?page=5",
"previous": "https://api.example.org/accounts/?page=3",
"results": [
…
]
}
💡settings.py 에서 REST_FRAMEWORK라는 변수 안에 페이지네이션을 따로 넣어주는구나!
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100
}
💡아 페이지네이션은 이걸 가져다가 쓰면 되겠구나!
class BillingRecordsView(generics.ListAPIView):
queryset = Billing.objects.all()
serializer_class = BillingRecordsSerializer
pagination_class = LargeResultsSetPagination
# views.py의 페이지네이션 클래스에 붙일 라이브러리
from rest_framework.pagination import PageNumberPagination
# views.py의 제너릭 뷰를 상속받기 위한 라이브러리
from rest_framework import generics
#setting.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 15,
}
#Archive view 작성
from rest_framework import viewsets
class ArchaivePaginationViewSet(viewsets.ModelViewSet):
queryset = ArchiveModel.objects.all()
serializer_class = ArchiveSerializer
pagination_class = PageNumberPagination
#urls.py
from .views import ArchaivePaginationViewSet
pagination = ArchaivePaginationViewSet.as_view({
'get': 'list',
'post': 'create'
})
urlpatterns = [
path('pagination/', pagination, name='archive_pagination'),
]
#setting.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 15,
}
#urls.py
urlpatterns = [
path('pagination/', views.ArchaivePagination.as_view(), name='archive_pagination'),
]
# Archive view 작성
class ArchaivePagination(generics.ListAPIView):
queryset = ArchiveModel.objects.all()
serializer_class = ArchiveSerializer
pagination_class = PageNumberPagination