Class-Based View (Django)

A class-based view (CBV) implements a Django view as a class instead of a function. Methods like get() and post() handle each HTTP method, and inheritance/mixins let views share behavior, reducing repetitive code.

Django

CBVs organize view logic by HTTP method: define get() for GET requests and post() for POST instead of branching on request.method. They subclass django.views.View (or a generic view) and are wired into URLs with View.as_view().

Their real power is reuse through inheritance and mixins. A LoginRequiredMixin enforces authentication, a custom base view can share querysets, and Django's generic views bundle common patterns so you write very little. The trade-off is a steeper learning curve, since logic is spread across inherited methods.

Function-based views remain perfectly valid and are often clearer for one-off, custom logic. Reach for CBVs (especially the generic ones) when your view follows a standard pattern like 'list these objects' or 'edit this object'.

Syntax

class MyView(View):
    def get(self, request): ...

Examples

Python

from django.views import View
from django.shortcuts import render

class AboutView(View):
    def get(self, request):
        return render(request, "about.html")

# urls.py
# path("about/", AboutView.as_view())

Learn it in a lesson

See also

Back to the Dictionary