How to use the pagination in Django REST Framework

  • 2 days ago
The REST framework provides customizable pagination features, allowing you to control the way large result sets are broken down into individual pages.

This video shows how i've implemented the Django REST Framework's pagination feature.
Transcript
00:00With Django REST framework, it's easy to create a pagination feature to list items.
00:16To list items by page, in my frontend, I'm using this HTTP request to request items by page.
00:31And in backend, I have this URL pattern which directs the request to the posts app where the request is handled.
01:02In this class, I'm using a PostsListSerializer which helps me to define which fields should be present in the list.
01:23I'm using this to request all posts from the database and this is the pagination class with pagination settings.
01:44This code is used to verify if the user is authenticated and this I will use later because it will be used to filter this request.
02:05I need to let the user to get only its posts.
02:12In this class, I extend the Django REST framework List API view which provides the features like this and this to list the items by pagination.
02:38This code will be used to apply the filters which I will have here.
02:50This code is used to order the posts by ID in descending order.
03:01And this code applies the pagination settings from this class.
03:17Next, I'm verifying if items or posts exist.
03:25Then, I'm getting the current page and using the Serializer to define the fields which should be in each post.
03:48And here is the response.
03:52Posts list, total number of posts and current page.

Recommended