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

ListView की मूल बातें

ListView Django का एक database table से हर item दिखाने वाला page अपने आप बनाने का तरीका है, एक shelf जैसा जो library की हर book list करता है।
Syntax
markup
from django.views.generic import ListView

class ModelNameListView(ListView):
    model = ModelName
    template_name = 'app_name/modelname_list.html'
    context_object_name = 'items'

एक Minimal ListView

एक ListView subclass पर model set करना Django के लिए उस model की हर row query करने और इसे template को एक list के रूप में pass करने के लिए काफी है, बिना किसी manual queryset code के।

उदाहरण: A Minimal ListView

BookListView automatically runs Book.objects.all() and renders it as the "books" context variable.

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


class BookListView(ListView):
    model = Book
    template_name = 'books/book_list.html'
    context_object_name = 'books'
{# 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. #}

Template में List Render करना

Template को queryset context_object_name (या default से object_list) से दिए नाम के under मिलता है और यह standard {% for %} template tag से इसके ऊपर loop कर सकता है।

उदाहरण: Rendering the List in a Template

The {% for %} tag iterates the books queryset passed in by BookListView, with {% empty %} handling the zero-results case.

markup
{% for book in books %}
  <p>{{ book.title }}</p>
{% empty %}
  <p>No books yet.</p>
{% endfor %}
{# 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. #}

Queryset Customize करना

get_queryset() को override करना आपको filter, order, या limit करने देता है कि ListView कौन से objects दिखाए, सिर्फ model attribute से हमेशा हर row दिखाने के बजाय।

Note: हमेशा get_queryset() से एक queryset (एक list नहीं) return करें ताकि pagination और आगे chaining अब भी काम करे।

उदाहरण: Customizing the Queryset

get_queryset() overrides the default Book.objects.all() with a filtered, ordered queryset.

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


class PublishedBookListView(ListView):
    model = Book

    def get_queryset(self):
        return Book.objects.filter(is_published=True).order_by('title')
{# 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. यह भूल जाना कि ListView का default context variable name object_list (या "<model_name>_list") है, फिर template में गलत नाम reference करना।
  2. template_name set न करना और Django की auto-generated default template path convention से surprised होना।
  3. get_queryset() को override करने के बजाय model = Model edit करके queryset filter करने की कोशिश करना।
चैप्टर सारांश
  • ListView एक manual view function लिखे बिना एक model या queryset से objects की एक list display करता है।
  • Context variable default से object_list होता है, पर context_object_name इसे rename कर सकता है।
  • कौन से objects दिखें filter, order, या अन्यथा customize करने के लिए get_queryset() को override करें।
  • ListView template_name set न होने तक अपने आप <app>/<model>_list.html नाम वाला एक template ढूंढता है।

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.