ListView की मूल बातें
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.
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.
{% 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 दिखाने के बजाय।
get_queryset() से एक queryset (एक list नहीं) return करें ताकि pagination और आगे chaining अब भी काम करे।उदाहरण: Customizing the Queryset
get_queryset() overrides the default Book.objects.all() with a filtered, ordered queryset.
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. #}
- यह भूल जाना कि ListView का default context variable name
object_list(या"<model_name>_list") है, फिर template में गलत नाम reference करना। template_nameset न करना और Django की auto-generated default template path convention से surprised होना।get_queryset()को override करने के बजायmodel = Modeledit करके 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_nameset न होने तक अपने आप<app>/<model>_list.htmlनाम वाला एक template ढूंढता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: