Answer the question
In order to leave comments, you need to log in
Why does pagination return all existing objects starting from the second page?
Made a standard pagination. The first page displays the results, the number of which is limited by the value entered in the view. But if you go to any other page, then all content is displayed, including previous and next.
def viewtrainers(request, slug):
trainers_list = Profile.objects.filter(city__slug = slug)
page = request.GET.get('page')
paginator = Paginator(trainers_list, 10)
try:
trainers = paginator.page(page)
except PageNotAnInteger:
trainers = paginator.page(1)
except EmptyPage:
trainers = paginator.page(paginator.num_pages)
return render(request, "trainers.html", {'trainers':trainers})
{% if trainers.has_other_pages %}
<ul class="pagination justify-content-center" style="margin:20px 0">
{% if trainers.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ trainers.previous_page_number }}">«</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link" >«</span></li>
{% endif %}
{% for i in trainers.paginator.page_range %}
{% if trainers.number == i %}
<li class="page-item active"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if trainers.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ trainers.next_page_number }}">»</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link" >»</span></li>
{% endif %}
</ul>
{% endif %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question