Challenge - 5 Problems
ListView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django ListView render?
Given the following ListView, what will be the name of the context variable containing the list of objects in the template?
Django
from django.views.generic import ListView from .models import Book class BookListView(ListView): model = Book template_name = 'books/book_list.html'
Attempts:
2 left
💡 Hint
Think about the default context variable name when only model is specified.
✗ Incorrect
When you specify only the model in a ListView, Django uses 'object_list' as the default context variable name for the list of objects.
❓ state_output
intermediate2:00remaining
How many items will be displayed by this ListView?
Assuming the Book model has 10 entries in the database, and the ListView is defined as below, how many books will appear on the page?
Django
from django.views.generic import ListView from .models import Book class BookListView(ListView): model = Book paginate_by = 3 template_name = 'books/book_list.html'
Attempts:
2 left
💡 Hint
Check the paginate_by attribute and how it limits items per page.
✗ Incorrect
The paginate_by attribute limits the number of items per page to 3, so only 3 books will be shown on the first page.
📝 Syntax
advanced2:00remaining
Which option correctly overrides the queryset in a ListView?
You want to display only books published after 2020. Which code correctly overrides the queryset method in a ListView?
Django
from django.views.generic import ListView from .models import Book class RecentBookListView(ListView): model = Book template_name = 'books/recent_books.html' def get_queryset(self): # Your code here pass
Attempts:
2 left
💡 Hint
Use Django ORM filter syntax with double underscores for field lookups.
✗ Incorrect
Option D correctly uses the Django ORM filter with double underscore syntax and accesses the model manager properly.
🔧 Debug
advanced2:00remaining
Why does this ListView raise an AttributeError?
Consider this ListView code snippet. Why does it raise 'AttributeError: 'NoneType' object has no attribute 'filter'' when accessed?
Django
from django.views.generic import ListView from .models import Book class FilteredBookListView(ListView): queryset = None def get_queryset(self): return self.queryset.filter(author='Alice')
Attempts:
2 left
💡 Hint
Check the value of queryset before calling filter on it.
✗ Incorrect
The queryset attribute is None, so calling filter on None causes the AttributeError.
🧠 Conceptual
expert2:00remaining
What is the effect of setting context_object_name in a ListView?
If you set context_object_name = 'books' in a ListView, what changes in the template rendering?
Attempts:
2 left
💡 Hint
Think about how context variables are named in templates.
✗ Incorrect
Setting context_object_name changes the variable name used in the template to access the list of objects.