ListView y DetailView
Muestre colecciones y registros individuales
ListView y DetailView es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 1 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.
Two Pages You Always Need
Nearly every app shows a list of things and a page for one thing. Django ships generic views that build both for you. ✨
Meet ListView
ListView fetches many objects and hands them to a template, so you skip writing the query and the render call yourself.
Point ListView at a Model
You give ListView a model, and it runs the query for every row in that table automatically.
from django.views.generic import ListView
from .models import Post
class PostList(ListView):
model = PostThe Default Template Name
ListView looks for a template named app/model_list.html, like post_list.html, unless you override it.
object_list in the Template
Inside the template the rows arrive as object_list, ready for you to loop over and display.
{% for post in object_list %}
<li>{{ post.title }}</li>
{% endfor %}Nicer Names with context_object_name
Set context_object_name so your template variable reads naturally, like posts instead of object_list.
class PostList(ListView):
model = Post
context_object_name = "posts"Meet DetailView
DetailView fetches a single object by its key and renders one focused page for it.
How DetailView Finds the Object
DetailView reads a pk or slug from the URL, then runs get() to load exactly that one record.
from django.views.generic import DetailView
class PostDetail(DetailView):
model = PostThe Detail Template and object
DetailView renders app/model_detail.html and exposes the record as object in the template.
<h1>{{ object.title }}</h1>
<p>{{ object.body }}</p>Wiring Them into URLs
Each generic view becomes a URL with as_view(), which turns the class into a callable view.
path("posts/", PostList.as_view()),
path("posts/<int:pk>/", PostDetail.as_view()),Less Code, Same Result
Together these two views replace dozens of lines of boilerplate, giving you list and detail pages almost for free. 🚀
Quick Check
Where does DetailView get the object it shows?
Recap: List and Detail
You met ListView for collections and DetailView for one record, wired both with as_view(), and skipped tons of boilerplate. 🎉
Preguntas frecuentes
¿La lección «ListView y DetailView» es gratis?
Sí — el texto completo de «ListView y DetailView» 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 «ListView y DetailView»?
Muestre colecciones y registros individuales 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 1 de 4.
¿Cuánto tiempo toma la lección «ListView y DetailView»?
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
- ListView y DetailView
- CreateView y UpdateView
- DeleteView y success_url
- Sobrescritura de get_queryset y Templates