0
0
Djangoframework~20 mins

DetailView for single objects in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DetailView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
ANo context variable is passed to the template
BA context variable named 'object_list' containing all Book instances
CA context variable named 'book_detail' containing the Book instance with the given pk
DA context variable named 'book' containing the Book instance with the given pk
Attempts:
2 left
💡 Hint
Think about the default context variable name DetailView uses for the model instance.
📝 Syntax
intermediate
2: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
A
def get_object(self):
    return Author.objects.filter(pk=self.kwargs['pk'])
B
def get_object(self):
    return Author.objects.get(pk=self.kwargs['pk'])
C
def get_object(self):
    return self.model.objects.get(id=self.request.GET['id'])
D
def get_object(self):
    return self.model.get(pk=self.kwargs['pk'])
Attempts:
2 left
💡 Hint
Remember get_object should return a single model instance, not a queryset.
state_output
advanced
2: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')
A404 Not Found error is raised
BThe first Author object is shown by default
CA TypeError is raised because pk is missing
DThe view redirects to the authors list page
Attempts:
2 left
💡 Hint
Think about what happens when the URL does not provide the required pk argument.
🔧 Debug
advanced
2: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')
AThe model Publisher does not have a 'name' field
BThe URL pattern does not pass 'name' to the view
CThere are multiple Publisher objects with the same name, so get() fails
Dget_object should return a queryset, not a single object
Attempts:
2 left
💡 Hint
get() expects exactly one object. What if multiple objects match the filter?
🧠 Conceptual
expert
2: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?
Aget_context_data
Bget_object
Cdispatch
Drender_to_response
Attempts:
2 left
💡 Hint
Think about where the context dictionary is prepared before rendering.