TemplateView e get_context_data
Renderize um template e adicione o contexto de forma organizada
TemplateView e get_context_data é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. ✨
Perguntas Frequentes
A aula “TemplateView e get_context_data” é grátis?
Sim — o texto completo de “TemplateView e get_context_data” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “TemplateView e get_context_data”?
Renderize um template e adicione o contexto de forma organizada Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “TemplateView e get_context_data”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- De Views de Função a Classes de View
- TemplateView e get_context_data
- Lidando com Métodos get e post
- Mixins e as_view()