CBVs में template_name और Context
template_name एक class-based view को बताता है कि कौन सा HTML page उपयोग करना है, और context वह data का box है जो यह उस page को blanks भरने के लिए देता है।In this page:
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.
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 किया जाए।
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.
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.
<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. #}
get_context_data()को override करना लेकिन पहलेsuper().get_context_data(**kwargs)call करना भूल जाना, जो वह data drop करता है जो parent class ने जोड़ा होता।- बिना जांचे यह मान लेना कि हर generic view same default context variable name उपयोग करता है (ListView vs DetailView अलग हैं)।
template_nameको एक typo के साथ hardcode करना और app के template folder जांचने के बजाय एक confusingTemplateDoesNotExisterror पाना।
- 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 जोड़ें।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: