ListViewとDetailView
コレクションと単一レコードを表示します
「ListViewとDetailView」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「ListViewとDetailView」で何を学びますか?
コレクションと単一レコードを表示します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応の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とTemplatesのオーバーライド