Performance: ListView for displaying collections
This affects page load speed and rendering performance by controlling how many items are fetched and rendered in the browser at once.
Jump into concepts and practice - no test required
from django.views.generic import ListView class ItemListView(ListView): model = Item template_name = 'items.html' paginate_by = 20 # Load 20 items per page
from django.views.generic import ListView class ItemListView(ListView): model = Item template_name = 'items.html' paginate_by = None # No pagination, loads all items at once
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No pagination (all items loaded) | Large number of nodes | Multiple reflows due to large DOM | High paint cost due to many elements | [X] Bad |
| Pagination with 20 items per page | Small number of nodes | Single reflow on page load | Low paint cost | [OK] Good |
ListView?ListView?class BookListView(ListView):
model = Book
paginate_by = 3
class AuthorListView(ListView):
model = Author
template = 'authors.html'
Product items but with the context variable named items instead of the default product_list. How do you customize the ListView to do this?