Skip to content

Instantly share code, notes, and snippets.

@mmospanenko
Created September 9, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmospanenko/2a160c27fc8969bb4967e65a7da56bcc to your computer and use it in GitHub Desktop.
Save mmospanenko/2a160c27fc8969bb4967e65a7da56bcc to your computer and use it in GitHub Desktop.
Django bootstrap 4 pagination template
{% if page.has_other_pages %}
<nav aria-label="Page navigation">
<ul class="pagination pg-dark justify-content-center">
{% if page.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#"><span>&laquo;</span></a></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page.number > 3 and forloop.first %}
<li class="page-item"><a class="page-link" href="?page=1">1</a></li>
<li class="page-item disabled"><a class="page-link" href="#"><span>...</span></a></li>
{% endif %}
{% if page.number == i %}
<li class="page-item active"><a class="page-link" href="#">{{ i }} <span class="sr-only">(current)</span></a>
</li>
{% elif i > page.number|add:'-3' and i < page.number|add:'3' %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% if page.paginator.num_pages > page.number|add:'3' and forloop.last %}
<li class="page-item disabled"><a class="page-link" href="#"><span>...</span></a></li>
<li class="page-item"><a class="page-link"
href="?page={{ page.paginator.num_pages }}">{{ page.paginator.num_pages }}</a></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#"><span>&raquo;</span></a></li>
{% endif %}
</ul>
</nav>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment