I have a view.py product_list:
...
from django.shortcuts import render, get_object_or_404
from .models import ProductCategory, Product, ProductDetail, ProductSpecialCategory
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
...
def product_list(request, category_slug=None, ):
category = None
categories = ProductCategory.objects.all()
object_list = Product.objects.filter(available=True, is_active=True)
if category_slug:
category = get_object_or_404(ProductCategory, slug=category_slug)
object_list = object_list.filter(category=category)
paginator = Paginator(object_list, 1)
page = request.GET.get('page')
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
return render(request, 'shop/products/list_by_category/product_list.html', {'category': category,
'categories': categories,
'products': products,
})
Based on this handler, I did pagination.html:
<nav aria-label="pagination" class="pagination_area">
<ul class="pagination">
{% if page.has_previous %}
<li class="page-item next">
<a class="page-link" href="?page={{ page.previous_page_number }}">
<i class="fa fa-angle-left" aria-hidden="true"></i>
</a>
</li>
{% endif %}
{% for i in page.paginator.page_range %}
{% if page.number == i %}
<li class="page-item focused"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% elif i > page.number|add:'-1' and i < page.number|add:'1' %}
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li class="page-item next">
<a class="page-link" href="?page={{ page.next_page_number }}">
<i class="fa fa-angle-right" aria-hidden="true"></i>
</a>
</li>
{% endif %}
</ul>
On the interface, I get the **result**:
I would like to organize in such a way that:
Show only three pages, the first of which is the previous one, the second is the current, the third is the next. And what is not included in this range are hidden by ellipses, for example.:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…