我有一个包含600个房源的Django项目。我得到一个非常长的分页,我想知道如何缩短分页。也许不显示所有页码并替换一些...我正在使用bootstrap v4和django 1.10。我知道有些模块我可以使用,但我想学习。
模板
{% if product.has_other_pages %}
<div class="col-md-12 text-center">
<nav aria-label="">
<ul class="pagination">
{% if product.has_previous %}
<li class="page-item"><a class="page-link"
href="?page={{ product.previous_page_number }}">«</a>
</li>
{% else %}
<li class="page-item disabled"><a class="page-link"><span>«</span></a></li>
{% endif %}
{% for i in product.paginator.page_range %}
{% if product.number == i %}
<li class="page-item active"><a class="page-link" href="#">{{ i }} <span
class="sr-only">(current)</span></a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if product.has_next %}
<li class="page-item"><a class="page-link" aria-label="Next"
href="?page={{ product.next_page_number }}">»</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link"><span>»</span></a></li>
{% endif %}
</ul>
</nav>
</div>
{% endif %}
视图
def products_all(request):
products = Product.objects.order_by("-date_added")
product_count = Category.objects.annotate(num_products=Count('product')).order_by('-num_products')[:5]
company_count = Company.objects.annotate(num_products=Count('product')).order_by('-num_products')[:5]
manufacturer_count = Manufacturer.objects.annotate(num_products=Count('product')).order_by('-num_products')[:5]
total_manufacturer = Manufacturer.objects.count()
total_company = Company.objects.count()
total_categories = Category.objects.count()
paginator = Paginator(products, 50)
page = request.GET.get('page')
try:
product = paginator.page(page)
except PageNotAnInteger:
product = paginator.page(1)
except EmptyPage:
product = paginator.page(paginator.num_pages)
template = "products/all.html"
context = {'product': product,
'products': products,
'product_count': product_count,
'company_count': company_count,
'manufacturer_count': manufacturer_count,
'total_categories': total_categories,
'total_company': total_company,
'total_manufacturer': total_manufacturer
}
return render(request, template, context)