Add (simple) Bootstrap 4 paginator helper tag.

This commit is contained in:
Casper V. Kristensen 2020-01-12 03:40:19 +01:00
parent b65df95138
commit d88c4632cd
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,49 @@
{% load base_extras %}
{% if is_paginated %}
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">&laquo;</span>
</li>
{% endif %}
{% paginator_pages paginator.page_range page_obj.number as pages %}
{% for i in pages %}
{% if page_obj.number == i %}
<li class="page-item active" aria-current="page">
<span class="page-link">
{{ i }}
<span class="sr-only">(current)</span>
</span>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">&raquo;</span>
</li>
{% endif %}
</ul>
</nav>
{% endif %}

View file

@ -8,3 +8,11 @@ def active(context, url_name):
if context["request"].resolver_match.url_name == url_name: if context["request"].resolver_match.url_name == url_name:
return "active" return "active"
return "" return ""
@register.simple_tag()
def paginator_pages(page_range: range, current_number: int, margin=5):
return range(
max(page_range.start, current_number - margin),
min(page_range.stop, current_number + margin + 1)
)