Skip to main content

Fast sequential objects pagination for Django

Project description

django-sequential-pagination

Paginate ordered Django querysets sequentially with “Next” button. Fully compatible with django-el-pagination (but doesn’t depend on it).

The pagination is performed by object ID (or any other set of fields that give strict linear order) rather than by page number. Instead of ?page=2, ?page=3, etc., it produces links like ?from=11, ?from=21 and so on. This gives the following benefits:

  • The pagination works extremely fast even on huge data sets. For example, on Postgres “normal” pagination may take seconds (or even minutes) on queries like ?page=1000000.

  • It prevents duplicates on next page when new data is injected at top and shifts page boundaries (this is especially important for AJAX pagination).

The drawback is that there is no navigation to arbitrary page number and no reverse navigation, it’s always only the link to “Next page” (or nothing at the last page).

Installation

pip install django-sequential-pagination

Usage

Add django_sequential_pagination to INSTALLED_APPS:

# settings.py

INSTALLED_APPS = [
        ...
        'django_sequential_pagination',
]

Pass an ordered queryset to the template:

# views.py

def recent_posts(request):
        return render(request, "blog/posts.html", {
                'posts': Post.objects.all().order_by('-time', '-id'),
        })

Make sure that the ordering always has a tie breaker as the last key, otherwise you may get duplicates on page boundaries.

Now, paginate objects in the template:

{% load pagination %}

{% paginate posts per_page=10 as page %}

{% for post in page.objects %}
        <div>Post #{{ post.id }}</div>
{% endfor %}

{% if page.next_page_url %}
        <a href="{{ page.next_page_url }}">Next</a>
{% endif %}

Settings

You can override the default settings in your settings.py:

SEQUENTIAL_PAGINATION_PER_PAGE = 20
SEQUENTIAL_PAGINATION_KEY = 'from'  # querystring key to use, as in ?from=XXXX

django-el-pagination

You can enable endless pagination with django-el-pagination by putting this in the page template:

{% paginate posts per_page=10 key='page' as page %}

{% for post in page.objects %}
        <div>Post #{{ post.id }}</div>
{% endfor %}

{% if page.next_page_url %}
        <nav class="endless_container">
                <ul class="pagination"><!-- Bootstrap v3 styles -->
                        <li>
                                <a class="endless_more" href="{{ page.next_page_url }}" rel="{{ page.key }}">Show more</a>
                        </li>
                </ul>
        </nav>
{% endif %}

<script>
        $.endlessPaginate({paginateOnScroll: true});
</script>

Make sure the pagination key (or SEQUENTIAL_PAGINATION_KEY) matches your AjaxListView.key. The defaults are different (from and page, respectively).

Jinja2

If Jinja2 is installed, django_sequential_pagination.templatetags.pagination will be a jinja2.contextfunction.

Additionally, if django_jinja is installed, it will be registered automatically as a global template function, so you can use it right away:

{% set page = paginate(posts, per_page=10) %}

{% for post in page.objects %}
        <div>Post #{{ post.id }}</div>
{% endfor %}

{% if page.next_page_url %}
        <a href="{{ page.next_page_url }}">Next</a>
{% endif %}

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-sequential-pagination-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

django_sequential_pagination-0.1.0-py2-none-any.whl (5.5 kB view details)

Uploaded Python 2

File details

Details for the file django-sequential-pagination-0.1.0.tar.gz.

File metadata

  • Download URL: django-sequential-pagination-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.18

File hashes

Hashes for django-sequential-pagination-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23df0aee38a9ab821212334e422ac9d87379611d64e0b0a00e372bd56013d1e4
MD5 df30f4ba9fd7e042c7e5128383e9e156
BLAKE2b-256 3f31ed7bcc10f3e29b29a443943d50f6cfe6de53a071ad0e211d6d25ad1b089d

See more details on using hashes here.

File details

Details for the file django_sequential_pagination-0.1.0-py2-none-any.whl.

File metadata

  • Download URL: django_sequential_pagination-0.1.0-py2-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.18

File hashes

Hashes for django_sequential_pagination-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 68bb716c0ace9c566ba53211dcd24a1b758487f0ff2e210b36d1d2a43bc5a86f
MD5 a5ebead5051d19fafa1bb65612c8b948
BLAKE2b-256 e85df98e61e612dc1353986e63b37dcac22a660844fc1b95609dbfa98a2dcc1d

See more details on using hashes here.

Supported by

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