0
0
Djangoframework~10 mins

DetailView for single objects in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the generic DetailView class from Django.

Django
from django.views import [1]
Drag options to blanks, or click blank then click option'
Ahttp
Bmodels
Cforms
Dgeneric
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from django.views.models instead of django.views.generic
Trying to import DetailView directly from django.views
2fill in blank
medium

Complete the code to define a DetailView for a model named Book.

Django
class BookDetailView([1]):
    model = Book
Drag options to blanks, or click blank then click option'
ADetailView
BView
CListView
DTemplateView
Attempts:
3 left
💡 Hint
Common Mistakes
Using ListView which shows multiple objects
Using TemplateView which does not handle models automatically
3fill in blank
hard

Fix the error in the URL pattern to use the DetailView correctly for BookDetailView.

Django
path('book/<int:pk>/', [1].as_view(), name='book-detail')
Drag options to blanks, or click blank then click option'
ABookDetailView
BBookListView
CBookCreateView
DBookUpdateView
Attempts:
3 left
💡 Hint
Common Mistakes
Using ListView or CreateView instead of DetailView
Forgetting to call as_view()
4fill in blank
hard

Fill both blanks to customize the template name and context object name in the DetailView.

Django
class BookDetailView(DetailView):
    model = Book
    template_name = '[1]'
    context_object_name = '[2]'
Drag options to blanks, or click blank then click option'
Abooks/book_detail.html
Bbook_detail.html
Cbook
Dbook_detail
Attempts:
3 left
💡 Hint
Common Mistakes
Using folder paths in template_name incorrectly
Setting context_object_name to the model class name instead of a string
5fill in blank
hard

Fill all three blanks to override the get_context_data method to add extra data to the template context.

Django
class BookDetailView(DetailView):
    model = Book

    def get_context_data(self, **kwargs):
        context = super().[1](**kwargs)
        context['[2]'] = 'Extra info'
        return context

    template_name = '[3]'
Drag options to blanks, or click blank then click option'
Aget_context_data
Bextra_info
Cbook_detail.html
Dget_queryset
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the wrong super method
Not returning the context dictionary
Forgetting to set the template_name