TemplateView y get_context_data
Renderice una plantilla y añada el contexto limpiamente
TemplateView y get_context_data es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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. ✨
Preguntas frecuentes
¿La lección «TemplateView y get_context_data» es gratis?
Sí — el texto completo de «TemplateView y get_context_data» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.
¿Qué aprenderé en «TemplateView y get_context_data»?
Renderice una plantilla y añada el contexto limpiamente Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Django Academy?
No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «TemplateView y get_context_data»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Django Academy?
Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- De vistas de funciones a clases de vista
- TemplateView y get_context_data
- Gestionar los métodos get y post
- Mixins y as_view()