← Back to Django Course | Chapter 9: Class-Based Views | Lesson 10 of 10

Django में Pagination

Pagination एक giant book को numbered pages में split करने जैसा है ताकि readers एक समय में सिर्फ एक page items देखें बजाय एक endless scroll के।
Syntax
markup
class ViewName(ListView):
    model = ModelName
    paginate_by = 10

{% if page_obj.has_previous %}<a href="?page={{ page_obj.previous_page_number }}">Previous</a>{% endif %}
{% if page_obj.has_next %}<a href="?page={{ page_obj.next_page_number }}">Next</a>{% endif %}

एक ListView को Paginate करना

एक ListView पर paginate_by set करना queryset को उस size के pages में split करने के लिए काफी है; Django data slice करना और एक ?page= query parameter से current page figure out करना अपने आप handle करता है।

उदाहरण: Paginating a ListView

paginate_by = 10 makes BookListView show 10 books per page, reading the page number from ?page= in the URL.

markup
from django.views.generic import ListView
from .models import Book


class BookListView(ListView):
    model = Book
    paginate_by = 10
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Page Navigation Render करना

जब paginate_by set हो, template को page_obj (current page represent करता हुआ) और is_paginated (True जब एक से ज़्यादा page हो) मिलता है, जो साथ में Previous/Next links बनाने के लिए काफी हैं।

उदाहरण: Rendering Page Navigation

page_obj.has_previous/has_next and their matching page numbers drive the Previous/Next links shown to the user.

markup
{% for book in page_obj %}
  <p>{{ book.title }}</p>
{% endfor %}

{% if is_paginated %}
  {% if page_obj.has_previous %}
    <a href="?page={{ page_obj.previous_page_number }}">Previous</a>
  {% endif %}
  {% if page_obj.has_next %}
    <a href="?page={{ page_obj.next_page_number }}">Next</a>
  {% endif %}
{% endif %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Paginator Class से Manual Pagination

एक function-based view के लिए, Django की Paginator class सीधे वही splitting logic प्रदान करती है: इसे एक queryset और एक page size दें, फिर इससे एक specific page मांगें।

उदाहरण: Manual Pagination with the Paginator Class

Paginator(Book.objects.all(), 10).get_page(...) produces the same page_obj a ListView would build automatically.

markup
from django.core.paginator import Paginator
from django.shortcuts import render
from .models import Book


def book_list(request):
    paginator = Paginator(Book.objects.all(), 10)
    page_obj = paginator.get_page(request.GET.get('page'))
    return render(request, 'books/book_list.html', {'page_obj': page_obj})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि paginate_by सिर्फ ListView पर अपने आप काम करता है — एक manual view को Paginator class सीधे उपयोग करनी चाहिए।
  2. Template में object_list पर loop करना यह expect करते हुए कि pagination active है बिना is_paginated जांचे या page_obj उपयोग किए।
  3. Template में "Previous"/"Next" links न जोड़ना, pagination को backend में काम करता हुआ लेकिन users के लिए unreachable छोड़ते हुए।
चैप्टर सारांश
  • Pagination एक long list of objects को सब कुछ एक साथ दिखाने के बजाय छोटे pages में split करता है।
  • ListView एक बार paginate_by एक page size पर set होने पर अपने आप paginate करता है।
  • Template को navigation बनाने के लिए page_obj (current page) और is_paginated (एक boolean) मिलता है।
  • Django की Paginator class same behavior के लिए function-based views में सीधे उपयोग की जा सकती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.