Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Commit 675f5f5

Browse files
committed
Bootstrap 4 support for Pagination
Bootstrap 4's Pagination code is backwards compatible with Bootstrap 3's. It adds two new class names (page-item and page-link) and the option of aligining the pagination bar using a FlexBox utility class name. So, these options have been added in a way that works for Bootstrap 3 and 4. Pager, however, is simply not supported on Bootstrap 4. It will be nearly 100% unstyled. It may be removed in a future release as a result.
1 parent edf7e70 commit 675f5f5

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ Finally, make sure that you have the request context processor enabled:
113113
and thus it is expected that there is a named "page" argument in the
114114
URL referenced by **url_view_name**. This allows us to use pretty
115115
pagination URLs such as "/page/1"
116+
- *extra_pagination_classes* - A space separated list of CSS class names that
117+
will be added to the top level <ul> HTML element.
118+
In particular, this can be utilized in Bootstrap 4
119+
installations to add the appropriate alignment
120+
classes from Flexbox utilities: eg:
121+
justify-content-center
122+
116123

117124
**Basic Usage**
118125

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ url_extra_kwargs
127127
there is a named "page" argument in the URL referenced by **url_view_name**. This allows
128128
us to use pretty pagination URLs such as "/page/1"
129129

130+
extra_pagination_classes
131+
A space separated list of CSS class names that will be added to the top level
132+
<ul> HTML element. In particular, this can be utilized in Bootstrap 4
133+
installations to add the appropriate alignment classes from Flexbox utilities,
134+
eg: justify-content-center
135+
130136
**Basic Usage**
131137

132138
The following will show a pagination bar with a link to every page, a previous link, and a next link:

bootstrap_pagination/templates/bootstrap_pagination/pagination.html

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
{% load bootstrap_pagination %}
2-
<ul class="pagination{% if size == "small" %} pagination-sm{% endif %}{% if size == "large" %} pagination-lg{% endif %}">
2+
<ul class="pagination{% if size == "small" %} pagination-sm{% endif %}{% if size == "large" %} pagination-lg{% endif %} {{ extra_pagination_classes }}">
33
{% if show_first_last %}
44
{% if not page.has_previous %}
5-
<li class="disabled">
6-
<span aria-hidden="true" title="First Page">{{ first_label }}</span>
5+
<li class="page-item disabled">
6+
<span class="page-link" aria-hidden="true" title="First Page">{{ first_label }}</span>
77
</li>
88
{% else %}
9-
<li>
10-
<a aria-label="First Page" title="First Page" href="{{ first_page_url|default:"#"|escape }}"><span aria-hidden="true">{{first_label}}</span></a>
9+
<li class="page-item">
10+
<a class="page-link" aria-label="First Page" title="First Page" href="{{ first_page_url|default:"#"|escape }}"><span aria-hidden="true">{{first_label}}</span></a>
1111
</li>
1212
{% endif %}
1313
{% endif %}
1414
{% if show_prev_next %}
1515
{% if not page.has_previous %}
16-
<li class="disabled">
17-
<span aria-hidden="true" title="Previous Page">{{ previous_label }}</span>
16+
<li class="page-item disabled">
17+
<span class="page-link" aria-hidden="true" title="Previous Page">{{ previous_label }}</span>
1818
</li>
1919
{% else %}
20-
<li>
21-
<a aria-label="Previous Page" title="Previous Page" href="{{ previous_page_url|default:"#"|escape }}"><span aria-hidden="true">{{ previous_label }}</span></a>
20+
<li class="page-item">
21+
<a class="page-link" aria-label="Previous Page" title="Previous Page" href="{{ previous_page_url|default:"#"|escape }}"><span aria-hidden="true">{{ previous_label }}</span></a>
2222
</li>
2323
{% endif %}
2424
{% endif %}
2525
{% for pagenum, index_range, url in page_urls %}
2626
{% if page.number == pagenum %}
27-
<li class="active">
28-
<span aria-label="Current Page" title="Current Page">{% if show_index_range %} {{ index_range }} {% else %} {{ pagenum }} {% endif %}</span>
27+
<li class="page-item active">
28+
<span class="page-link" aria-label="Current Page" title="Current Page">{% if show_index_range %} {{ index_range }} {% else %} {{ pagenum }} {% endif %}</span>
2929
</li>
3030
{% else %}
31-
<li>
32-
<a aria-label="Page {{ pagenum }} of {{ page.paginator.num_pages }}" title="Page {{ pagenum }} of {{ page.paginator.num_pages }}" href="{{ url|escape }}">{% if show_index_range %} {{ index_range }} {% else %} {{ pagenum }} {% endif %}</a>
31+
<li class="page-item">
32+
<a class="page-link" aria-label="Page {{ pagenum }} of {{ page.paginator.num_pages }}" title="Page {{ pagenum }} of {{ page.paginator.num_pages }}" href="{{ url|escape }}">{% if show_index_range %} {{ index_range }} {% else %} {{ pagenum }} {% endif %}</a>
3333
</li>
3434
{% endif %}
3535
{% endfor %}
3636
{% if show_prev_next %}
3737
{% if not page.has_next %}
38-
<li class="disabled">
39-
<span aria-hidden="true" title="Next Page">{{ next_label }}</span>
38+
<li class="page-item disabled">
39+
<span class="page-link" aria-hidden="true" title="Next Page">{{ next_label }}</span>
4040
</li>
4141
{% else %}
42-
<li>
43-
<a aria-label="Next Page" title="Next Page" href="{{ next_page_url|default:"#"|escape }}"><span aria-hidden="true">{{ next_label }}</span></a>
42+
<li class="page-item">
43+
<a class="page-link" aria-label="Next Page" title="Next Page" href="{{ next_page_url|default:"#"|escape }}"><span aria-hidden="true">{{ next_label }}</span></a>
4444
</li>
4545
{% endif %}
4646
{% endif %}
4747
{% if show_first_last %}
4848
{% if not page.has_next %}
49-
<li class="disabled">
50-
<span aria-hidden="true" title="Last Page" >{{ last_label }}</span>
49+
<li class="page-item disabled">
50+
<span class="page-link" aria-hidden="true" title="Last Page" >{{ last_label }}</span>
5151
</li>
5252
{% else %}
53-
<li>
54-
<a aria-label="Last Page" title="Last Page" href="{{ last_page_url|default:"#"|escape }}"><span aria-hidden="true">{{last_label}}</span></a>
53+
<li class="page-item">
54+
<a class="page-link" aria-label="Last Page" title="Last Page" href="{{ last_page_url|default:"#"|escape }}"><span aria-hidden="true">{{last_label}}</span></a>
5555
</li>
5656
{% endif %}
5757
{% endif %}

bootstrap_pagination/templatetags/bootstrap_pagination.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ def render(self, context):
201201
url_get_params = kwargs.get("url_get_params", context['request'].GET)
202202
url_anchor = kwargs.get("url_anchor", None)
203203

204+
extra_pagination_classes = kwargs.get("extra_pagination_classes", "")
205+
204206
# Generage our viewable page range
205207
page_count = page.paginator.num_pages
206208
current_page = page.number
@@ -271,7 +273,8 @@ def render(self, context):
271273
'first_page_url': first_page_url,
272274
'last_page_url': last_page_url,
273275
'previous_page_url': previous_page_url,
274-
'next_page_url': next_page_url
276+
'next_page_url': next_page_url,
277+
'extra_pagination_classes': extra_pagination_classes,
275278
}))
276279

277280

@@ -329,8 +332,8 @@ def bootstrap_paginate(parser, token):
329332
the URL.
330333
331334
url_extra_args - This is used only in conjunction with url_name.
332-
When referencing a URL, additional arguments may be
333-
passed in as a list.
335+
When referencing a URL, additional arguments may be
336+
passed in as a list.
334337
335338
url_extra_kwargs - This is used only in conjunction with url_name.
336339
When referencing a URL, additional named arguments
@@ -341,6 +344,13 @@ def bootstrap_paginate(parser, token):
341344
filters.
342345
343346
url_anchor - The anchor to use in URLs. Defaults to None.
347+
348+
extra_pagination_classes - A space separated list of CSS class names
349+
that will be added to the top level <ul>
350+
HTML element. In particular, this can be
351+
utilized in Bootstrap 4 installatinos to
352+
add the appropriate alignment classes from
353+
Flexbox utilites, eg: justify-content-center
344354
"""
345355
bits = token.split_contents()
346356
if len(bits) < 2:

tests/test_paginate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_example(self):
3131
c = Context({'page_obj': paginator.page(2),
3232
'request': django.http.HttpRequest()})
3333
html = lxml.html.fragment_fromstring(template.render(c))
34-
self.assertEqual(html.get('class'), 'pagination')
34+
self.assertEqual(html.get('class').strip(), 'pagination')
3535
self.assertEqual(
3636
html.cssselect('[title=\"Current Page\"]')[0].text.strip(),
3737
'2')

0 commit comments

Comments
 (0)