ListView & DetailView (Django)

ListView and DetailView are Django's most-used generic views. ListView renders a list of objects (with built-in pagination), and DetailView renders a single object looked up by primary key or slug from the URL.

Django

ListView displays many objects: set model = Post and it queries Post.objects.all(), passes them to a template as object_list (or a name you choose via context_object_name), and supports pagination by setting paginate_by. Override get_queryset() to filter or order.

DetailView displays one object: it reads a pk or slug from the URL, fetches that object (raising 404 if missing), and passes it to the template. The URL pattern supplies the lookup, e.g. path('post/<int:pk>/', PostDetail.as_view()).

Both follow template-name conventions (modelname_list.html and modelname_detail.html) so you often do not even set template_name. They are the fastest way to build standard read pages, and they pair with CreateView/UpdateView/DeleteView for full CRUD.

Syntax

class PostList(ListView): model = Post; paginate_by = 10

Examples

Python

from django.views.generic import ListView, DetailView
from .models import Post

class PostList(ListView):
    model = Post
    paginate_by = 10

class PostDetail(DetailView):
    model = Post
# urls: path("post/<int:pk>/", PostDetail.as_view())

Learn it in a lesson

See also

Back to the Dictionary