-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pagination information and make display configurable via settings.
- Add SHOW_PAGINATION_INFO setting to toggle display of pagination info. - Update template and context overriding bootstrap4 templatetag.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{% load bootstrap4 %} | ||
{% with bpurl=bootstrap_pagination_url|default:"" %} | ||
<div class="d-flex align-items-baseline"> | ||
<ul class="{{ pagination_css_classes }}"> | ||
<li class="prev page-item{% if current_page == 1 %} disabled{% endif %}"> | ||
<a class="page-link" href="{% if current_page == 1 %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name 1 %}{% endif %}"> | ||
« | ||
</a> | ||
</li> | ||
|
||
{% if pages_back %} | ||
<li class="page-item"> | ||
<a class="page-link" href="{% bootstrap_url_replace_param bpurl parameter_name pages_back %}">…</a> | ||
</li> | ||
{% endif %} | ||
|
||
{% for p in pages_shown %} | ||
<li class="page-item{% if current_page == p %} active{% endif %}"> | ||
<a class="page-link" href="{% if current_page == p %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name p %}{% endif %}"> | ||
{{ p }} | ||
</a> | ||
</li> | ||
{% endfor %} | ||
|
||
{% if pages_forward %} | ||
<li class="page-item"> | ||
<a class="page-link" href="{% bootstrap_url_replace_param bpurl parameter_name pages_forward %}">…</a> | ||
</li> | ||
{% endif %} | ||
|
||
<li class="last page-item{% if current_page == num_pages %} disabled{% endif %}"> | ||
<a class="page-link" href="{% if current_page == num_pages %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name num_pages %}{% endif %}"> | ||
» | ||
</a> | ||
</li> | ||
|
||
</ul> | ||
{% if show_pagination_info %} | ||
<span class="ms-2 small">Showing {{ start_index }}-{{ end_index }} of {{ total_count }}</span> | ||
{% endif %} | ||
</div> | ||
{% endwith %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Any | ||
|
||
from bootstrap4.templatetags.bootstrap4 import get_pagination_context, register | ||
from django.conf import settings | ||
from django.core.paginator import Page | ||
|
||
|
||
@register.inclusion_tag("bootstrap4_overrides/pagination.html") | ||
def bootstrap_pagination(page: Page, **kwargs) -> dict[str, Any]: | ||
"""Render a Bootstrap pagination component with additional context. This function | ||
extends the default pagination context to include the start index, end index, and | ||
total count of items. | ||
Parameters | ||
---------- | ||
page : `Page` | ||
The page of results to show. | ||
Returns | ||
------- | ||
`dict` | ||
The context for rendering the pagination component, including | ||
pagination details and item counts. | ||
""" | ||
pagination_kwargs = kwargs.copy() | ||
pagination_kwargs["page"] = page | ||
|
||
# Get the default pagination context. | ||
context = get_pagination_context(**pagination_kwargs) | ||
|
||
# Add item counts to the context. | ||
context["start_index"] = page.start_index() | ||
context["end_index"] = page.end_index() | ||
context["total_count"] = page.paginator.count | ||
context["show_pagination_info"] = getattr(settings, "SHOW_PAGINATION_INFO", True) | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters