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

CBVs में template_name और Context

template_name एक class-based view को बताता है कि कौन सा HTML page उपयोग करना है, और context वह data का box है जो यह उस page को blanks भरने के लिए देता है।
Syntax
markup
class ViewName(ListView):
    model = ModelName
    template_name = 'app_name/template.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['extra_key'] = value
        return context

template_name को Explicitly Set करना

template_name Django के automatic template-path guess को override करता है, आपको एक class-based view को किसी भी template file की ओर point करने देते हुए जो आप चुनें।

उदाहरण: Setting template_name Explicitly

template_name tells BookListView to render catalog/all_books.html instead of the default books/book_list.html.

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


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

get_context_data के साथ Extra Context जोड़ना

get_context_data() को override करना एक class-based view को default object या queryset से अलग additional variables template को pass करने देता है, जब तक parent का context पहले preserve किया जाए।

Note: नए keys जोड़ने से पहले हमेशा super().get_context_data(**kwargs) call करें, या आप built-in context खो देंगे जो view normally provide करता है।

उदाहरण: Adding Extra Context with get_context_data

The template can now use {{ total_books }} alongside the usual object_list, because both are in the returned dict.

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


class BookListView(ListView):
    model = Book

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['total_books'] = Book.objects.count()
        return context
{# 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 में Extra Context उपयोग करना

get_context_data() के अंदर जोड़ी गई कोई भी key एक normal template variable बन जाती है, जिसे standard {{ }} syntax से उपयोग किया जा सकता है बिल्कुल किसी भी built-in context value जैसा।

उदाहरण: Using the Extra Context in a Template

total_books comes from the overridden get_context_data(); object_list still comes from ListView's default behavior.

markup
<p>Total books in catalog: {{ total_books }}</p>
{% for book in object_list %}
  <p>{{ book.title }}</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. #}
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. get_context_data() को override करना लेकिन पहले super().get_context_data(**kwargs) call करना भूल जाना, जो वह data drop करता है जो parent class ने जोड़ा होता।
  2. बिना जांचे यह मान लेना कि हर generic view same default context variable name उपयोग करता है (ListView vs DetailView अलग हैं)।
  3. template_name को एक typo के साथ hardcode करना और app के template folder जांचने के बजाय एक confusing TemplateDoesNotExist error पाना।
चैप्टर सारांश
  • template_name explicitly set करता है कि एक class-based view कौन सी template file render करे।
  • बिना template_name के, Django app और model name के आधार पर एक default path guess करता है।
  • get_context_data() वह method है जिसे override करना चाहिए जब एक template को main object या queryset से आगे extra data चाहिए।
  • इसे override करते समय हमेशा पहले super().get_context_data(**kwargs) call करें, फिर returned dict में नई keys जोड़ें।

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.