๐Ÿ“ Django

Pagination in Django ListView

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
353
๐ŸŒฟ
Level
Medium

Covered topics: Enabling pagination, Template with pagination, Pagination with filters, Manual pagination (in function-based views).

Enabling pagination

from django.views.generic import ListView
from .models import Task

class TaskListView(ListView):
    model = Task
    template_name = 'tasks/task_list.html'
    context_object_name = 'tasks'
    paginate_by = 20  # objects per page
    ordering = ['-created_at']

Template with pagination

{% for task in tasks %}
  <div>{{ task.title }}</div>
{% endfor %}

{% if is_paginated %}
<nav>
  {% if page_obj.has_previous %}
    <a href="?page={{ page_obj.previous_page_number }}">โ† Previous</a>
  {% endif %}

  <span>{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</span>

  {% if page_obj.has_next %}
    <a href="?page={{ page_obj.next_page_number }}">Next โ†’</a>
  {% endif %}
</nav>
{% endif %}

Pagination with filters

<!-- Preserve GET parameters when navigating between pages -->
<a href="?{{ request.GET.urlencode }}&page={{ page_obj.next_page_number }}">Next</a>

Manual pagination (in function-based views)

from django.core.paginator import Paginator

def task_list(request):
    tasks = Task.objects.all().order_by('-created_at')
    paginator = Paginator(tasks, 20)
    page_number = request.GET.get('page', 1)
    page_obj = paginator.get_page(page_number)

    return render(request, 'tasks/list.html', {'page_obj': page_obj})

ListView context variables

Variable Value
page_obj current page
paginator paginator object
is_paginated True if more than 1 page
object_list objects on the current page

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐Ÿ“

DRF ViewSets and Routers

ViewSet is a class that combines several related views into one. Router automatically generates URLs.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 283
๐Ÿ“

Django: Production Checklist

Before deploying a Django project, review its security, database, static files, HTTPS, logging, and backup...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 291
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum