Challenge - 5 Problems
DetailView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django DetailView render?
Given this DetailView and URL pattern, what will the template receive as context variable?
Django
from django.views.generic.detail import DetailView from .models import Book class BookDetailView(DetailView): model = Book template_name = 'books/detail.html' # URL pattern: # path('books/<int:pk>/', BookDetailView.as_view(), name='book-detail')
Attempts:
2 left
💡 Hint
Think about the default context variable name DetailView uses for the model instance.
✗ Incorrect
The DetailView automatically passes the single object as a context variable named after the model in lowercase. Here, it is 'book'.
📝 Syntax
intermediate2:00remaining
Which option correctly overrides get_object in a DetailView?
You want to customize how the object is fetched in a DetailView. Which code snippet correctly overrides get_object?
Django
from django.views.generic.detail import DetailView from .models import Author class AuthorDetailView(DetailView): model = Author def get_object(self): # custom logic here pass
Attempts:
2 left
💡 Hint
Remember get_object should return a single model instance, not a queryset.
✗ Incorrect
Option B correctly uses get() with pk from kwargs. Option B uses GET parameters incorrectly. Option B returns a queryset, not an object. Option B calls get on the model class incorrectly.
❓ state_output
advanced2:00remaining
What is the output of this DetailView with missing pk in URL?
Consider this DetailView and URL pattern. What happens if you visit '/authors/' without a pk?
Django
from django.views.generic.detail import DetailView from .models import Author class AuthorDetailView(DetailView): model = Author # URL pattern: # path('authors/<int:pk>/', AuthorDetailView.as_view(), name='author-detail')
Attempts:
2 left
💡 Hint
Think about what happens when the URL does not provide the required pk argument.
✗ Incorrect
The URL pattern requires a pk. Without it, Django cannot resolve the URL to this view, so a 404 error occurs.
🔧 Debug
advanced2:00remaining
Why does this DetailView raise MultipleObjectsReturned error?
This DetailView code raises MultipleObjectsReturned. Why?
Django
from django.views.generic.detail import DetailView from .models import Publisher class PublisherDetailView(DetailView): model = Publisher def get_object(self): return Publisher.objects.get(name=self.kwargs['name']) # URL pattern: # path('publishers/<str:name>/', PublisherDetailView.as_view(), name='publisher-detail')
Attempts:
2 left
💡 Hint
get() expects exactly one object. What if multiple objects match the filter?
✗ Incorrect
get() raises MultipleObjectsReturned if more than one object matches the query. Here, multiple publishers share the same name.
🧠 Conceptual
expert2:00remaining
How to customize context data in a DetailView?
You want to add extra data to the template context in a DetailView. Which method should you override?
Attempts:
2 left
💡 Hint
Think about where the context dictionary is prepared before rendering.
✗ Incorrect
get_context_data allows you to add extra variables to the context passed to the template.