An Introduction to Class‑Based Views in Django: Structure, Usage, and Advanced Features

This article explains how class‑based views (CBVs) work in Django, how they differ from function‑based views, how to use them in URL configurations, how to subclass and extend them, how to support additional HTTP methods such as HEAD, and how to build asynchronous class‑based views. It provides a clear overview of Django’s CBV architecture and practical examples for real‑world applications.

Django class-based views, CBVTemplateViewListView, async views

~4 min read • Updated Mar 14, 2026

Introduction

In Django, a view is any callable that receives an HTTP request and returns a response. While views are often written as simple functions, Django also provides a powerful system of class‑based views (CBVs). These allow developers to structure views using object‑oriented principles such as inheritance and mixins, making code more reusable and maintainable.


What Are Class‑Based Views?

Class‑based views are Python classes that implement view logic through methods. All CBVs inherit from Django’s base View class, which handles URL integration, HTTP method dispatching, and other shared functionality. Django also includes several generic CBVs for common tasks, such as rendering templates or listing objects.


Basic Examples of Built‑In CBVs

Django provides several foundational CBVs:

  • View: the base class for all CBVs.
  • RedirectView: returns an HTTP redirect.
  • TemplateView: renders a template.

Using CBVs in URLconf

The simplest way to use a class‑based view is to call its as_view() method directly in your URL configuration:


from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path("about/", TemplateView.as_view(template_name="about.html")),
]

Arguments passed to as_view() override class attributes, making it easy to customize behavior without subclassing.


Subclassing Generic Views

A more powerful approach is to subclass an existing CBV and override attributes or methods. For example, to create a dedicated view for an about.html page:


# some_app/views.py
from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = "about.html"

Then add it to your URLconf:


# urls.py
from django.urls import path
from some_app.views import AboutView

urlpatterns = [
    path("about/", AboutView.as_view()),
]

Supporting Additional HTTP Methods

CBVs make it easy to support multiple HTTP methods by defining methods such as get(), post(), put(), or head(). Here’s an example using HEAD to optimize API responses for a book list:


from django.http import HttpResponse
from django.views.generic import ListView
from books.models import Book

class BookListView(ListView):
    model = Book

    def head(self, *args, **kwargs):
        last_book = self.get_queryset().latest("publication_date")
        response = HttpResponse(
            headers={
                "Last-Modified": last_book.publication_date.strftime(
                    "%a, %d %b %Y %H:%M:%S GMT"
                )
            }
        )
        return response

A GET request returns the full list of books, while a HEAD request returns only headers, allowing clients to check whether new data exists without downloading the entire response.


Asynchronous Class‑Based Views

Django supports asynchronous CBVs using async def method handlers. This is useful for I/O‑bound operations such as external API calls.


import asyncio
from django.http import HttpResponse
from django.views import View

class AsyncView(View):
    async def get(self, request, *args, **kwargs):
        await asyncio.sleep(1)
        return HttpResponse("Hello async world!")

Important: all user‑defined handlers in a single CBV must be either synchronous or asynchronous. Mixing def and async def will raise an ImproperlyConfigured error.


Conclusion

Class‑based views provide a structured, reusable, and powerful way to build views in Django. Whether you’re rendering templates, handling forms, building APIs, or leveraging asynchronous operations, CBVs offer a flexible foundation for organizing your application’s logic. By understanding how to subclass, override methods, and support additional HTTP methods, you can create clean and maintainable view architectures.

Written & researched by Dr. Shahin Siami