DetailView Basics
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".
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).
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.
<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. #}
- Forgetting that DetailView needs either a pk or a slug in the URL pattern to know which object to fetch.
- Assuming the context variable is always "object" when context_object_name has been customized elsewhere.
- Using DetailView on a queryset with no matching row and being surprised by the automatic 404 instead of a crash.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: