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

template_name and Context in CBVs

template_name tells a class-based view which HTML page to use, and context is the box of data it hands to that page to fill in the blanks.

Setting template_name Explicitly

template_name overrides Django's automatic template-path guess, letting you point a class-based view at any template file you choose.

Example: 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. #}

Adding Extra Context with get_context_data

Overriding get_context_data() lets a class-based view pass additional variables to the template beyond the default object or queryset, as long as the parent's context is preserved first.

Note: Always call super().get_context_data(**kwargs) before adding new keys, or you lose the built-in context the view normally provides.

Example: 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. #}

Using the Extra Context in a Template

Any key added inside get_context_data() becomes a normal template variable, usable with the standard {{ }} syntax exactly like any built-in context value.

Example: 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. #}
Common Mistakes
  1. Overriding get_context_data() but forgetting to call super().get_context_data(**kwargs) first, which drops the data the parent class would have added.
  2. Assuming every generic view uses the same default context variable name without checking (ListView vs DetailView differ).
  3. Hardcoding template_name with a typo and getting a confusing TemplateDoesNotExist error instead of checking the app's template folder.
Chapter Summary
  • template_name explicitly sets which template file a class-based view renders.
  • Without template_name, Django guesses a default path based on the app and model name.
  • get_context_data() is the method to override when a template needs extra data beyond the main object or queryset.
  • Always call super().get_context_data(**kwargs) first when overriding it, then add new keys to the returned dict.

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.