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 }}">«</a></li>
{% else %}
<li class="disabled"><span>«</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 }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}