Creating function to call api for pagination is calling duplicate value int
Question:
I am building a simple app in react
and django
in which I am accessing API
and paginating in the backend.
The Problem is that, I want to make two calls on per page click, for example – If user clicked on page 1 then I am trying to make api call with page &page=1
and &page=2
.
And If user clicked on page 2 then make api call &page=3
and &page=4
and If user clicked on page 3 then make api call &page=5
and &page=6
But the problem is that When user is on page 1st
then it is making api calls (page 1 and 2) finely But when User moves to page 2 then it is calling api with &page=2
and &page=3
instead of &page=3
and &page=4
.
I want to call api for next two pages.
views.py
class MakeApiCall(APIView):
def get(self, *args, **kwargs):
current_page = self.request.query_params.get('current_page')
current_page = int(current_page)
for index in range(2):
url = "https://api.punkapi.com/v2/beers?page=" + str(current_page)
call_api = requests.get(url)
print(call_api.url)
current_page += 1
When I run above function with current_page = 1 then It is calling api with page=1
and page=2
but when I call current_page = 2 then It is calling again with page=2
and page=3
not page=3
and page=4
I have created while loop just for it But It is working just like this range function.
Every page returns 25 results
so I will stop the loop when Array has 50 items But It is still working just as below function
results = []
while len(results) != 50:
url = "https://api.punkapi.com/v2/beers?page=" + str(current_page)
call_api = requests.get(url)
for result in api_results.json():
if len(results) < 50:
results.append(result)
else:
break
But It is still calling the api the same way before.
I liked the while
method, It would be best but not working.
Frontend is fine but if you need it then please let me know
Answers:
It looks like index
isn’t used in the for
loop to fetch the appropriate pages. Instead, you’re fetching the current page and the next one, which is the behavior you observed. In the while
loop, current_page
is fetched 50 times, without any incrementation.
We can use a simple formula for determining the start index of the loop: current_page * 2 - 1
(you can manually check that this works for the cases where current_page
is 1, 2, 3, etc.).
(The formula works because on each call to the API, you’re fetching a pair of pages, starting from the odd-numbered page.)
This means the loop in views.py
should look like the following:
...
for i in range(2):
start_index = current_page * 2 - 1
url = "https://api.punkapi.com/v2/beers?page=" + str(start_index + i)
call_api = requests.get(url)
print(call_api.url)
You should calculate
index1 = (current_page * 2) - 1
index2 = (current_page * 2)
For current_page = 1
it gives 1, 2
For current_page = 2
it gives 3, 4
For current_page = 3
it gives 5, 6
etc.
I am building a simple app in react
and django
in which I am accessing API
and paginating in the backend.
The Problem is that, I want to make two calls on per page click, for example – If user clicked on page 1 then I am trying to make api call with page &page=1
and &page=2
.
And If user clicked on page 2 then make api call &page=3
and &page=4
and If user clicked on page 3 then make api call &page=5
and &page=6
But the problem is that When user is on page 1st
then it is making api calls (page 1 and 2) finely But when User moves to page 2 then it is calling api with &page=2
and &page=3
instead of &page=3
and &page=4
.
I want to call api for next two pages.
views.py
class MakeApiCall(APIView):
def get(self, *args, **kwargs):
current_page = self.request.query_params.get('current_page')
current_page = int(current_page)
for index in range(2):
url = "https://api.punkapi.com/v2/beers?page=" + str(current_page)
call_api = requests.get(url)
print(call_api.url)
current_page += 1
When I run above function with current_page = 1 then It is calling api with page=1
and page=2
but when I call current_page = 2 then It is calling again with page=2
and page=3
not page=3
and page=4
I have created while loop just for it But It is working just like this range function.
Every page returns 25 results
so I will stop the loop when Array has 50 items But It is still working just as below function
results = []
while len(results) != 50:
url = "https://api.punkapi.com/v2/beers?page=" + str(current_page)
call_api = requests.get(url)
for result in api_results.json():
if len(results) < 50:
results.append(result)
else:
break
But It is still calling the api the same way before.
I liked the while
method, It would be best but not working.
Frontend is fine but if you need it then please let me know
It looks like index
isn’t used in the for
loop to fetch the appropriate pages. Instead, you’re fetching the current page and the next one, which is the behavior you observed. In the while
loop, current_page
is fetched 50 times, without any incrementation.
We can use a simple formula for determining the start index of the loop: current_page * 2 - 1
(you can manually check that this works for the cases where current_page
is 1, 2, 3, etc.).
(The formula works because on each call to the API, you’re fetching a pair of pages, starting from the odd-numbered page.)
This means the loop in views.py
should look like the following:
...
for i in range(2):
start_index = current_page * 2 - 1
url = "https://api.punkapi.com/v2/beers?page=" + str(start_index + i)
call_api = requests.get(url)
print(call_api.url)
You should calculate
index1 = (current_page * 2) - 1
index2 = (current_page * 2)
For current_page = 1
it gives 1, 2
For current_page = 2
it gives 3, 4
For current_page = 3
it gives 5, 6
etc.