Pagination

Paginate large datasets - automatically for QuerySet properties, or manually with Django's Paginator.

Basic usage

PREVIEW
  • Item 1
  • Item 2
  • Item 3
  • Item 4
Page 1 of 3
class PaginationDemo(Component):
    page: int = 1

    @computed
    def total_pages(self):
        return (len(ITEMS) + PER_PAGE - 1) // PER_PAGE

    @computed
    def visible(self):
        start = (self.page - 1) * PER_PAGE
        return ITEMS[start:start + PER_PAGE]

    def prev(self):
        if self.page > 1:
            self.page -= 1

    def next(self):
        if self.page < self.total_pages:
            self.page += 1

    def inline_template(self):
        return """
        <div class="...">
            <ul class="...">
                {% for item in visible %}<li class="...">{{ item }}</li>{% endfor %}
            </ul>
            <div class="...">
                <button lace:click="prev" {% if page == 1 %}disabled{% endif %}>Prev</button>
                <span class="...">Page {{ page }} of {{ total_pages }}</span>
                <button lace:click="next" {% if page == total_pages %}disabled{% endif %}>Next</button>
            </div>
        </div>
        """

Automatic QuerySet pagination

A property holding a QuerySet is paginated automatically. Drive it with <name>_page and <name>_per_page properties (default 15 per page) - the template just iterates the property and receives the current page.

from django.db.models import QuerySet

class PostList(Component):
    posts: QuerySet = None
    posts_page: int = 1
    posts_per_page: int = 10

    def mount(self):
        self.posts = Post.objects.order_by('-created_at')

    def next_page(self):
        self.posts_page += 1

Load more

Append the next page instead of replacing - accumulate into a list and track whether more remain.

from django.core.paginator import Paginator

class Feed(Component):
    posts: list = []
    page: int = 1
    has_more: bool = True

    def mount(self):
        self.load()

    def load(self):
        page_obj = Paginator(Post.objects.all(), 20).get_page(self.page)
        self.posts.extend(list(page_obj))
        self.has_more = page_obj.has_next()

    def load_more(self):
        self.page += 1
        self.load()

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink