📝 Django

Pagination in Django ListView

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
92
🌿
Level
Medium

Охватываемые темы: 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

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 88
📝

HTML Lists

Lists are one of the most common markup elements. Navigation menus, post tags, how-to steps,...

📅 30.06.2026 👁️ 90

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!