Skip to main content

Generate pagination links in Django Templates

Project description

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

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 %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-smart-pagination-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

django_smart_pagination-1.0.0-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-smart-pagination-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django-smart-pagination-1.0.0.tar.gz
Algorithm Hash digest
SHA256 745a3cc51607138b82c92d0a0b6185d1217f1ec436fd4a6b6b672c8e2d5741e5
MD5 0124eb37b94639bb25133769172dc109
BLAKE2b-256 8506192d4d510363cf0c254ce25e6217e80af3aa187f02b74cfe9e77d79c6694

See more details on using hashes here.

File details

Details for the file django_smart_pagination-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_smart_pagination-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 18233a267e304de1b13cc29748b58277149bebad9a200e0370a9df673fb49f63
MD5 25cf38a2be91e6a79b9fec22a888857b
BLAKE2b-256 3b91e68fab577ecba947a439caa3302d83be293f009cfa486e9f0513900070dd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page