Skip to content

Instantly share code, notes, and snippets.

@luxu
Last active December 18, 2019 18:40
Show Gist options
  • Save luxu/a3a6a6f764b32f5cb7a1e41517a99ff9 to your computer and use it in GitHub Desktop.
Save luxu/a3a6a6f764b32f5cb7a1e41517a99ff9 to your computer and use it in GitHub Desktop.
example pagination in Django
<div class="row text-center">
<div class="col-lg-12">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">&laquo;</a></li>
{% endif %}
{% for pg in page_obj.paginator.page_range %}
<!-- Sempre mostra as 3 primeiras e 3 últimas páginas -->
{% if pg == 1 or pg == 2 or pg == 3 or pg == page_obj.paginator.num_pages or pg == page_obj.paginator.num_pages|add:'-1' or pg == page_obj.paginator.num_pages|add:'-2' %}
{% if page_obj.number == pg %}
<li class="page-item active"><a class="page-link" href="?page={{ pg }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">{{ pg }}</a>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ pg }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">{{ pg }}</a>
</li>
{% endif %}
{% else %}
{% if page_obj.number == pg %}
<li class="page-item active"><a class="page-link" href="?page={{ pg }}">{{ pg }}</a></li>
{% elif pg > page_obj.number|add:'-4' and pg < page_obj.number|add:'4' %}
<!-- Mostra 3 páginas antes e 3 páginas depois da atual -->
<li class="page-item"><a class="page-link" href="?page={{ pg }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">{{ pg }}</a></li>
{% elif pg == page_obj.number|add:'-4' or pg == page_obj.number|add:'4' %}
<li class="page-item"><a class="page-link" href="">...</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}&q={{ request.GET.q }}">&raquo;</a></li>
{% endif %}
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment