TemplateView and get_context_data
Render a template and add context cleanly.
TemplateView and get_context_data is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet TemplateView
TemplateView is a generic CBV whose only job is to render a template. It is the simplest class-based view you will use.
Set template_name
You tell TemplateView which file to render with the template_name attribute. No get method needed for a plain page.
class AboutView(TemplateView):
template_name = "about.html"Wire It Into urls
Add it to urls with as_view(), just like any CBV. That one line gives you a fully working page.
path("about/", AboutView.as_view())Passing Data to Templates
A static page is fine, but usually you need context: data your template can display. CBVs provide a clean hook for that.
Override get_context_data
The get_context_data method builds the dictionary passed to your template. Override it to add your own values.
Always Call super() First
Start by calling super() to get the default context, then add to it. Skipping this can drop data Django expects.
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return contextAdd Your Own Keys
After super(), insert any keys you need into the context dict. The template reads them by name.
context["year"] = 2024
return contextTemplates Read Context Keys
In the template, reference a context key with double braces. Whatever you put under year appears where you write it.
kwargs Carries URL Data
The kwargs argument holds values captured from the URL, like an id. You can read them while building context.
Less Boilerplate Than Functions
Compared to a function view, TemplateView removes the repetitive render call. You declare the template once and add only the data.
A Common First CBV
TemplateView plus get_context_data is the pattern you will reach for whenever a page needs a template and a bit of dynamic data.
Quick Check
Which method do you override to add data for a TemplateView?
Recap: TemplateView
You learned TemplateView: set template_name to render a page, and override get_context_data, calling super first, to pass data along. ✨
Frequently asked questions
Is the “TemplateView and get_context_data” lesson free?
Yes — the full text of “TemplateView and get_context_data” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “TemplateView and get_context_data”?
Render a template and add context cleanly. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “TemplateView and get_context_data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- From Function Views to View Classes
- TemplateView and get_context_data
- Handling get and post Methods
- Mixins and as_view()