← Back to Django Course | Chapter 9: Class-Based Views | Lesson 4 of 10

DetailView Basics

DetailView automatically builds a single page that shows one specific item in full detail, like opening one book to read its page instead of just seeing the shelf.

A Minimal DetailView

DetailView needs a model and a URL pattern that captures either pk or slug, so it knows exactly which single row to fetch and display.

Example: A Minimal DetailView

BookDetailView fetches one Book row using the pk from the URL and passes it to the template as "book".

markup
from django.views.generic import DetailView
from .models import Book


class BookDetailView(DetailView):
    model = Book
    template_name = 'books/book_detail.html'
    context_object_name = 'book'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Matching the URL Pattern

The URL pattern must include a <int:pk> or <slug:slug> converter matching how DetailView looks up the object, otherwise Django raises an error when the view runs.

Example: Matching the URL Pattern

The <int:pk> segment supplies the primary key DetailView uses internally to run Book.objects.get(pk=pk).

markup
from django.urls import path
from .views import BookDetailView

urlpatterns = [
    path('books/<int:pk>/', BookDetailView.as_view(), name='book-detail'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Rendering Object Details in a Template

Once DetailView finds the object, its fields are accessible in the template through dot notation on the context variable.

Example: Rendering Object Details in a Template

Each {{ book.field }} expression reads an attribute straight off the single Book instance DetailView supplied.

markup
<h1>{{ book.title }}</h1>
<p>Author: {{ book.author }}</p>
<p>Published: {{ book.published_date }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting that DetailView needs either a pk or a slug in the URL pattern to know which object to fetch.
  2. Assuming the context variable is always "object" when context_object_name has been customized elsewhere.
  3. Using DetailView on a queryset with no matching row and being surprised by the automatic 404 instead of a crash.
Chapter Summary
  • DetailView shows a single object, matched from the URL using a pk or slug keyword argument.
  • The object is available in the template as object or the name set via context_object_name.
  • A missing object automatically raises Http404 instead of an unhandled exception.
  • template_name defaults to <app>/<model>_detail.html when not explicitly set.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.