0

I have a Django project with 600 listings. I get an extremely long pagination and I would like to know how I would shorten the pagination. Maybe not show all the page numbers and replace some with ... I am using bootstrap v4 and django 1.10. I know there are modules out there I could use but I want to learn.

template

   {% 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 }}">&laquo;</a>
                                </li>
                            {% else %}
                                <li class="page-item disabled"><a class="page-link"><span>&laquo;</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 }}">&raquo;</a></li>
                            {% else %}
                                <li class="page-item disabled"><a class="page-link"><span>&raquo;</span></a></li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
            {% endif %}

views

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)
Tom Myers
  • 159
  • 1
  • 13
  • Possible duplicate of [Django pagination...slicing pages to show fraction of total pages?](http://stackoverflow.com/questions/5426525/django-pagination-slicing-pages-to-show-fraction-of-total-pages). Also [Display only some of the page numbers by django pagination](http://stackoverflow.com/questions/30864011/display-only-some-of-the-page-numbers-by-django-pagination). – solarissmoke Mar 26 '17 at 03:57
  • The second link was what I was looking for thank you. When I searched that one did not come up. – Tom Myers Mar 26 '17 at 11:14
  • I was able to fix with the help of this answer [http://stackoverflow.com/questions/30864011/display-only-some-of-the-page-numbers-by-django-pagination] – Tom Myers Mar 26 '17 at 11:15
  • Possible duplicate of [Display only some of the page numbers by django pagination](https://stackoverflow.com/questions/30864011/display-only-some-of-the-page-numbers-by-django-pagination) – John Moutafis Jun 06 '17 at 09:35

0 Answers0