0
0
Djangoframework~20 mins

ListView for displaying collections in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ListView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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'
Abook_list
Bbooks
Cobject_list
Dbook_objects
Attempts:
2 left
💡 Hint
Think about the default context variable name when only model is specified.
state_output
intermediate
2: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'
A10
B3
CNone (all books)
D0
Attempts:
2 left
💡 Hint
Check the paginate_by attribute and how it limits items per page.
📝 Syntax
advanced
2: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
Areturn self.model.objects.filter(published_year > 2020)
Breturn self.get_queryset().filter(published_year__gt=2020)
Creturn Book.objects.filter(published_year > 2020)
Dreturn Book.objects.filter(published_year__gt=2020)
Attempts:
2 left
💡 Hint
Use Django ORM filter syntax with double underscores for field lookups.
🔧 Debug
advanced
2: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')
ABecause queryset is set to None, so calling filter on None causes the error.
BBecause get_queryset should not be defined when queryset is set.
CBecause author='Alice' is invalid syntax in filter.
DBecause ListView requires model attribute when queryset is None.
Attempts:
2 left
💡 Hint
Check the value of queryset before calling filter on it.
🧠 Conceptual
expert
2: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?
AThe list of objects will be available in the template as 'books' instead of the default 'object_list'.
BThe template name will automatically change to 'books.html'.
CThe ListView will only display objects of type 'books'.
DThe pagination will be disabled automatically.
Attempts:
2 left
💡 Hint
Think about how context variables are named in templates.