Skip to content

Instantly share code, notes, and snippets.

@luiscberrocal
Created July 8, 2018 14:09
Show Gist options
  • Save luiscberrocal/daf91d7c1a8388812d1f84a704667fb8 to your computer and use it in GitHub Desktop.
Save luiscberrocal/daf91d7c1a8388812d1f84a704667fb8 to your computer and use it in GitHub Desktop.
Django Pagination Snippet Bootstap 4
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a href="?page={{ page_obj.previous_page_number }}&order_by={{ order_by }}&direction={{ direction }}"
class="page-link">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% else %}
<li class="disabled">
<a class="page-link" href="#">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
{% if page_obj.number|add:'-4' > 1 %}
<li class="page-item">
<a href="?page={{ page_obj.number|add:'-5' }}&order_by={{ order_by }}&direction={{ direction }}"
class="page-link">&hellip;</a>
</li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active">
<a class="page-link" href="#">{{ i }} <span class="sr-only">(current)</span></a>
</li>
{% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %}
<li class="page-item">
<a href="?page={{ i }}&order_by={{ order_by }}&direction={{ direction }}" class="page-link">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.paginator.num_pages > page_obj.number|add:'4' %}
<li class="page-item">
<a href="?page={{ page_obj.number|add:'5' }}&order_by={{ order_by }}&direction={{ direction }}"
class="page-link">&hellip;</a>
</li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item">
<a href="?page={{ page_obj.next_page_number }}&order_by={{ order_by }}&direction={{ direction }}"
class="page-link">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
{% else %}
<li class="disabled page-item">
<a class="page-link" href="#">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment