An Introduction to Django Class‑Based Views: Structure, Usage, Subclassing, HTTP Methods, and Async Support

This article provides a complete introduction to Django’s class‑based views (CBVs). It explains how CBVs work, how to use them in URLconf, how to subclass and customize them, how to support additional HTTP methods such as HEAD, and how to write asynchronous class‑based views using async/await.

Class-Based Views، Django CBV، TemplateViewRedirectView، AsyncViewHEAD method، URLconf

~3 min read • Updated Mar 14, 2026

Introduction

In Django, a view is any callable that takes a request and returns a response. While views are often written as functions, Django also supports class‑based views (CBVs), which allow developers to structure their logic using inheritance and mixins. CBVs help you write reusable, organized, and maintainable code.


Basic Class‑Based Views

Django provides several base view classes suitable for many applications. All CBVs inherit from the View class, which handles URL integration, HTTP method dispatching, and other common behaviors.

Common base views include:

  • View: the foundation of all CBVs
  • TemplateView: renders a template
  • RedirectView: performs an HTTP redirect

Using CBVs in URLconf

The simplest way to use a CBV is to call as_view() directly in your URLconf. Any arguments passed to as_view() override attributes on the class.


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

Subclassing Generic Views

A more powerful approach is to subclass an existing view and override attributes or methods. For example, to display a static template:


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

Then add it to your URLconf:


path("about/", AboutView.as_view()),

Supporting Additional HTTP Methods

CBVs automatically support GET and POST, but you can add other HTTP methods such as HEAD. This is useful for APIs where clients may want metadata without downloading the full response.

Example: Adding HEAD to a ListView


class BookListView(ListView):
    model = Book

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

Behavior:

  • GET: returns the full list of books
  • HEAD: returns only headers, no body

Asynchronous Class‑Based Views

Django supports asynchronous views using async def. This allows you to perform non‑blocking I/O operations inside your view.

Example: Async View


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

Important rules:

  • All handlers in a view must be either synchronous or asynchronous
  • Mixing def and async def raises ImproperlyConfigured
  • Django automatically runs async views in an async context

Why Use Class‑Based Views?

  • Cleaner, reusable code
  • Powerful inheritance and mixin support
  • Built‑in support for many HTTP methods
  • Async support for high‑performance applications
  • Easy customization through overriding methods

Conclusion

Class‑based views provide a structured, extensible, and powerful way to build Django applications. Whether you’re rendering templates, handling redirects, supporting additional HTTP methods, or writing asynchronous logic, CBVs give you the flexibility and clarity needed for modern web development.

Written & researched by Dr. Shahin Siami