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

Related Articles

Django Tasks Framework: A Complete Guide to Background Task Execution in Django 6.0

Django 6.0 introduces the Tasks framework, a built‑in system for defining and queuing background work outside the request–response cycle. This article explains how Tasks work, how to configure backends, how to define and enqueue tasks, how context works, and how to integrate third‑party worker systems for production environments.

Continue

Asynchronous Support in Django: A Complete Guide to Async Views, ORM, Middleware, Performance, and Safety

This article explains Django’s asynchronous (async) capabilities, including async views, ASGI support, middleware behavior, async ORM features, performance considerations, handling disconnects, and Django’s async safety protections. It also covers how to use sync_to_async(), async ORM methods, and how to safely run synchronous code in async environments.

Continue

Django System Check Framework: A Complete Guide to Writing, Registering, Running, and Testing System Checks

This article explains Django’s System Check Framework—a powerful mechanism for detecting configuration issues, validating project structure, and ensuring code quality. It covers how checks are executed, how to write custom checks, how messages work, how to register and tag checks, how to extend checks for fields and models, and how to write both unit and integration tests for system checks.

Continue

Django Signals: A Complete Guide to Listening, Connecting, Sending, and Managing Application Events

This article provides a comprehensive explanation of Django’s signal system—an event‑driven mechanism that allows decoupled applications to react to actions occurring elsewhere in the framework. It covers how to define receivers, connect signals, use decorators, handle specific senders, organize signal code, and follow best practices to avoid complexity.

Continue

Understanding Django Settings: Configuration, Environment Management, and Best Practices

This article provides a complete overview of Django’s settings system. It explains how settings files work, how to designate a settings module, how to use settings in your code, how to configure Django manually, how to secure sensitive settings, and how to work with custom default settings. It also covers the role of django.setup() for standalone scripts.

Continue

Serializing and Deserializing Django Objects: A Complete Guide to Django’s Serialization Framework

This article explains Django’s serialization framework, including how to serialize and deserialize model instances, work with subsets of fields, handle inherited models, use different serialization formats (JSON, XML, YAML, JSONL), and understand how relational fields are represented. It also covers DeserializedObject behavior and common pitfalls.

Continue