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).
Unlike “normal” pagination with ?page=1, ?page=2, etc., this library:
Works extremely fast even on huge data sets. For example, on Postgres “normal” pagination takes seconds (or even minutes) on queries like ?page=1000000.
Prevents duplicates on next page if new data was injected at top and shifted page boundaries (this is especially important for AJAX pagination).
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 view_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 template tag, 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
Hashes for django-sequential-pagination-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c615f674db01f2414d77646c40af99bf786605e257304620ab4ec82d70331936 |
|
MD5 | bfe6b72e2ef519b4689a055ae7b32ccb |
|
BLAKE2b-256 | d0f9811b8026bc10328b0c0c41c4a64dee1eb570561313f0888161a91eb9a474 |