Django में Pagination
In this page:
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.
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.
{% 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.
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. #}
- यह भूल जाना कि
paginate_byसिर्फ ListView पर अपने आप काम करता है — एक manual view को Paginator class सीधे उपयोग करनी चाहिए। - Template में
object_listपर loop करना यह expect करते हुए कि pagination active है बिनाis_paginatedजांचे याpage_objउपयोग किए। - 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 में सीधे उपयोग की जा सकती है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: