title: How to shorten Pagination in Django
body: <p>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.</p>
  
  <p>template</p>
  
  <pre><code>   {% if product.has_other_pages %}
                  &lt;div class="col-md-12 text-center"&gt;
                      &lt;nav aria-label=""&gt;
                          &lt;ul class="pagination"&gt;
                              {% if product.has_previous %}
                                  &lt;li class="page-item"&gt;&lt;a class="page-link"
                                                           href="?page={{ product.previous_page_number }}"&gt;&amp;laquo;&lt;/a&gt;
                                  &lt;/li&gt;
                              {% else %}
                                  &lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;&lt;span&gt;&amp;laquo;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                              {% endif %}
                              {% for i in product.paginator.page_range %}
                                  {% if product.number == i %}
                                      &lt;li class="page-item active"&gt;&lt;a class="page-link" href="#"&gt;{{ i }} &lt;span
                                              class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                                  {% else %}
                                      &lt;li class="page-item"&gt;&lt;a class="page-link" href="?page={{ i }}"&gt;{{ i }}&lt;/a&gt;&lt;/li&gt;
                                  {% endif %}
                              {% endfor %}
                              {% if product.has_next %}
                                  &lt;li class="page-item"&gt;&lt;a class="page-link" aria-label="Next"
                                                           href="?page={{ product.next_page_number }}"&gt;&amp;raquo;&lt;/a&gt;&lt;/li&gt;
                              {% else %}
                                  &lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;&lt;span&gt;&amp;raquo;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                              {% endif %}
                          &lt;/ul&gt;
                      &lt;/nav&gt;
                  &lt;/div&gt;
              {% endif %}
  </code></pre>
  
  <p>views</p>
  
  <pre><code>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)
  </code></pre>