Reputation: 654
i have a problem with django.
I have created a pagination function and everything is fine, but when i try to enter to the last page of the pagination, i get the error "EmptyPage this page does not contain results"
.
But that page really exists! there are items left but it doesn´t show me the five last items of the Query.
Here is my function:
def clasification(request):
categoria = Clasificacion.objects.filter(existencia=True)
paginator = Paginator(categoria, 5)
page = request.GET.get('page')
try:
items = paginator.page(page)
except PageNotAnInteger:
items = paginator.page(1)
except EmptyPage:
items = paginator.page(paginator.num_pages)
contexto = {'meta_description':'',
'meta_keywords':'',
'items':items}
return render(request, 'adminview/clasification.html', contexto)
Everything seems to be fine here...
Take a look of the HTML:
{% if items.has_next or items.has_previous %}
<ul class="pagination">
{% if items.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ items.previous_page_number }}">Anterior</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link">Anterior</a></li>
{% endif %}
{% for page in items.paginator.page_range %}
<li class="page-item {% if items.number == page %}active{% endif %}"><a class="page-link" href="?page={{ page }}">{{ page }}</a></li>
{% endfor %}
{% if items.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
{% endif %}
</ul>
{% endif %}
I dont know why i´m getting this error.
Hope you can help me.
Thank you!.
Upvotes: 0
Views: 425
Reputation: 1101
In your template, you are trying to use items.next_page_number
when items.has_next
is false.
This will solve your problem:
{% if items.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link">Siguiente</a></li>
{% endif %}
Upvotes: 2