6.30.15 - HOW CAN I MAKE THIS QUESTION BETTER AND MORE HELPFUL TO OTHERS? FEEDBACK WOULD BE HELPFUL. THANKS!
I am using DRF as server side to a Dojo/Dgrid web application. Dojo needs a content-range or range response from the server. Currently it doesn't send any and so the pagination for dgrid.grid isn't working properly.
In the DRF documentation it states" Pagination links that are included in response headers, such as Content-Range or Link." But doesn't give a clear process on HOW to set the DRF API to do this. I am still relatively new to web app development. Any help would be appreciated!
DRF recommends a thirdparty package in its documentation: django-rest-framework-link-header-pagination
It is supposed to follow the same path as the Github API, which is basically the proper way of doing what the other answer suggest.
Here is a sample Link header taken from Github's API guide:
I haven't tried the package yet but I'll report back once done.
1. Including
Link
Header in response:To include a
Link
header in your response, you need to create a custom pagination serializer class, This should subclasspagination.BasePagination
and override theget_paginated_response(self, data)
method.Example (taken from docs):
Suppose we want to replace the default pagination output style with a modified format that includes the next and previous links in a
Link
header, we override theget_paginated_response()
.After this, we need to include this pagination class in our settings so that it is used by DRF instead of the default pagination classes.
API responses for list endpoints will now include a
Link
header.2. Including
Content-Range
Header in response:You can also send
Content-Range
header instead ofLink
. Just create a headers dictionary withContent-Range
as the key and value as how many items are being returned and how many total items exist.For example:
Suppose this is the header received:
This tells us that the response has a
Content-Range
header with value asitems 0-9/50
. This indicates that first 10 items are returned out of total50
.You can also use
*
instead of total no. of items if calculating total is expensive.