728x90
Django 페이지네이션 구현 코드
Params
limit = int(request.query_params.get('limit', '15'))
page = int(request.query_params.get('page', 1))
offset = max(page -1, 0 ) * limit
next_offset = max(page, 1) * limit
Use offset
Use offset for list slicing if there are objects or lists
# You Can use this pattern to objects or another list
review_list = [review for owner_place in owner_places for review in owner_place._places.review_list]
current_review_list = review_list[offset: offset + limit]
next_review_list = review_list[next_offset: next_offset + limit]
NextUrl
next_url = None
if next_review_list:
next_url = request.build_absolute_uri().split('?')[0] + '?'
next_url += f'page={page + 1}'
next_url += f'&limit={limit}'
Return
return Response(
dict(
next=next_url,
review_list=current_review_list
)
)