Skip to main content

Generate pagination links for Django

Instead of displaying links to all the pages at once, django-smart-pagination calculates a limited subset of them.

Works with Django Templates and Jinja2.

Usage

First add smart_pagination to your installed apps:

INSTALLED_APPS = [
    'smart_pagination'
]

Use a pagination block passing the Page object, the number of links (literal of variable) and the name to associate the smart_pagination.Paginator object.

{% load pagination_tags %}
{% paginate page_obj num_links paging %}
<ul>
    {% for page in paging.pages %}
    <li class="{% if page.is_current %}current{% endif %}">{{ page.number }}</li>
    {% endfor %}
</ul>
{% endpaginate %}

The Paginator object contains the following properties:

first

First Page. Will be None if it is already the first page.

prev

Previous Page. Will be None if there is no previous page.

pages

List of pages.

next

Next Page. Will be None if there is no next page.

last

Last Page. Will be None if it is already the last page.

{% load pagination_tags %}
{% paginate page_obj num_links paging %}
<ul>
    {% if paging.first %}
    <li>First - {{ paging.first.number }}</li>
    {% endif %}

    {% if paging.prev %}
    <li>Previous - {{ paging.prev.number }}</li>
    {% endif %}

    {% for page in paging.pages %}
    <li class="{% if page.is_current %}current{% endif %}">{{ page.number }}</li>
    {% endfor %}

    {% if paging.next %}
    <li>Next - {{ paging.next.number }}</li>
    {% endif %}

    {% if paging.last %}
    <li>Last - {{ paging.last.number }}</li>
    {% endif %}
</ul>
{% endpaginate %}

If you are sending the page_kwarg as a query parameter, you can optionally pass a fourth argument with the name of the page_kwarg and the Paginator will provide the query string without the page_kwarg:

{% load pagination_tags %}
{% paginate page_obj num_links paging 'page' %}
<ul>
    {% for page in paging.pages %}
    <li><a href="?page={{ page.number }}&{{ paging.query }}">{{ page.number }}</a></li>
    {% endfor %}
</ul>
{% endpaginate %}

Release files for django-smart-pagination 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-smart-pagination 1.0.1
File Size Uploaded
django-smart-pagination-1.0.1.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-smart-pagination 1.0.1
File Interpreter ABI Platform
django_smart_pagination-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.0 kB

Release files / django-smart-pagination-1.0.1.tar.gz

Download URL django-smart-pagination-1.0.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e36adedd406fbcb3776f88c5c16a564938c426904423f847274d526a6123d4d9
BLAKE2b-256 checksum
How to use checksums
d67f97008db56406cd4d1f871c61cb34843ea9f9e19b9f8a893996801a6f544c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_smart_pagination-1.0.1-py2.py3-none-any.whl

Download URL django_smart_pagination-1.0.1-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d6f388227cd967d1ae6b153d517b5e0faf90a4fea3ca28a192a0c183d4f45bed
BLAKE2b-256 checksum
How to use checksums
1c26a76eeddbbe3ad20492641c91e5360dc1ec15e7be8bdcefb0fb1a45044963
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page