ListView e DetailView
Exiba coleções e registros individuais
ListView e DetailView é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 1 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.
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. 🎉
Perguntas Frequentes
A aula “ListView e DetailView” é grátis?
Sim — o texto completo de “ListView e DetailView” é 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 “ListView e DetailView”?
Exiba coleções e registros individuais 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 1 de 4.
Quanto tempo leva a aula “ListView e DetailView”?
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
- ListView e DetailView
- CreateView e UpdateView
- DeleteView e success_url
- Substituindo get_queryset e Templates