0

I am about to add a pagination to my list of contacts. I am sitting over it the whole day and have no idea what I mixed up. Important thing is that I do have a working filters - so I can narrow the list. But from my understanding pagination should work anyway. In my case I see nothing so my guess is first "if" fails. If you could point me in the right direction. Best regards.

Views.py

def ContactsList(request):

    contacts_list = Contact.objects.all()
    Contacts_filter = LFilter(request.GET, queryset=contacts_list)

    #pagination
    page = request.GET.get('page', 1)
    paginator = Paginator(contacts_list, 20)

    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
    return render(request, 'index.html', context, {'filter': contacts_filter})

Template part:

{% if contacts.has_other_pages %}
  <ul class="pagination">
    {% if contacts.has_previous %}
      <li><a href="?page={{ contacts.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in contacts.paginator.page_range %}
      {% if library.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if contacts.has_next %}
      <li><a href="?page={{ contacts.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}
Jan Mejor
  • 141
  • 3
  • 15

1 Answers1

0

problem with this line:

return render(request, 'index.html', context, {'filter': contacts_filter})

here you are passing the context as filter only and missing contacts

so change this to

return render(request, 'index.html', context, {'filter': contacts_filter,'contacts': contacts})
Thameem
  • 3,426
  • 4
  • 26
  • 32
  • Must be something more, since I still do not see any pagination. :( And I do have more then 5k objects there. – Jan Mejor Apr 24 '17 at 06:57
  • Right ... got it to work. Is should be: return render(request, 'index.html', context = {'filter': contacts_filter,'contacts': contacts}) – Jan Mejor Apr 24 '17 at 07:16