J
J
Jekson2018-08-05 13:45:45
Django
Jekson, 2018-08-05 13:45:45

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})

templates.html
{% 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 }}">&laquo;</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link" >&laquo;</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 }}">&raquo;</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link" >&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

If you return from any page to the first one, then it also displays all the objects.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question