ListView 与 DetailView
显示集合和单条记录
ListView 与 DetailView 是 CoddyKit 上的免费 Django Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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. 🎉
常见问题解答
「ListView 与 DetailView」课时是免费的吗?
是的 — 「ListView 与 DetailView」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「ListView 与 DetailView」这节课中我会学到什么?
显示集合和单条记录 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「ListView 与 DetailView」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ListView 与 DetailView
- CreateView 与 UpdateView
- DeleteView 与 success_url
- 重写 get_queryset 与模板