I have a pagination in Django application. when I load the page the data display is fine on the first page but when I click on second button of paginator no data is shown on next page. Following is my code:
viwes.py
def get_queryset(self, **kwargs):
# get query params
query = self.request.GET.get("q", "")
page = self.request.GET.get("page", "1")
size = self.request.GET.get("rows", "15")
cname = self.request.GET.getlist("cname", [])
tags = self.request.GET.getlist("tag", [])
filter_param = self.request.GET.getlist("filter_param", [])
# get size and set to default 15 if its not digit
if size.isdigit():
size = int(size)
else:
size = 15
# get response from elastic search
response = self.get_archives(query, page, size, cname, tags, filter_param)
response = list(iter(response))
for res in response:
if 'statement' in res:
ref = self.get_reference(res['statement'], response)
res['references'] = ref
p = Paginator(response, 2)
pages = p.get_page(page)
return pages
response list is passed to frontend, response has data when sending it to first page but when request is send again after clicking on second button response list is passed empty to frontend.
claims.html
<div class="row" style="padding-top: 30px;">
{%if archives.paginator.count %}
<div class="col" style="float: right;">
<nav aria-label="Page navigation">
<ul class="pagination">
{% if archives.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1&q={{query}}&size={{size}}&cname={{cname}}"
aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">begin</span>
</a>
</li> {% endif %}
{% for n in archives.paginator.page_range %}
{% if archives.number == n %}
<li class="page-item active">
<span class="page-link">{{ n }}<span class="sr-only">(current)</span></span>
</li>
{% elif n > archives.number|add:'-3' and n < archives.number|add:'3' %}
<li class="page-item"><a class="page-link"
href="?page={{ n }}&q={{query}}&size={{size}}&cname={{cname}}">{{ n }}</a></li>
{% endif %}
{% endfor %}
{% if archives.has_next %}
<li class="page-item">
<a class="page-link"
href="?page={{ archives.paginator.num_pages }}&q={{query}}&size={{size}}&cname={{cname}}"
aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">end</span>
</a>
</li>
{% endif %} </ul>
</nav>
</div>
{%else%}
<h6 class="light-grey">No Results Found</h6>
{%endif%}
<div class="col">
<div class="float-right">
Showing results {{ archives.start_index }} to {{ archives.end_index }} of
{{ archives.paginator.count }}
</div>
</div>
</div>
Append
{{ request.get_full_path }}
to the beginning of the next and previous buttonsOld
New
Worked for me.