Class-Based ListView Basics

A starter ListView with model, template, and context name.


# models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=120)
    author = models.CharField(max_length=100)
  

# views.py
from django.views.generic import ListView
from .models import Book

class BookList(ListView):
    model = Book
    template_name = "books/list.html"
    context_object_name = "books"
  

# templates/books/list.html
<ul>
  {% for book in books %}
  <li>{{ book.title }} by {{ book.author }}</li>
  {% endfor %}
</ul>

# urls.py
from django.urls import path
from .views import BookList

urlpatterns = [
    path("books/", BookList.as_view(), name="book-list"),
]
  
Explanation:
  • Set context_object_name to customize the template variable name (default is object_list).
  • ListView automatically queries all objects from the model and passes them to the template.
  • Category Views (FBV & CBV)
  • Total Views 485
  • Last Modified 29 November, 2025
  • Tags #cbv #listview #generic #views
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.