Class Based vs Function Based Views (original) (raw)

Last Updated : 11 May, 2026

A core feature of Django is views, which define how data is presented to users. Views can be implemented in two main ways: Function-Based Views (FBVs) and Class-Based Views (CBVs).

Function-Based Views (FBVs)

FBVs are simple Python functions that receive a request, process it and return a response. They provide a straightforward way to handle HTTP requests.

**Pros of FBVs:

**Cons of FBVs:

**Example: Creating a function-based view

Python `

from django.http import HttpResponseRedirect from django.urls import reverse from django.shortcuts import render from .forms import MyForm

def example_create_view(request): template_name = 'form.html' form_class = MyForm

form = form_class()

if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('list-view'))

return render(request, template_name, {'form': form})

`

**In this example:

Class-Based Views (CBVs)

CBVs are Python classes that handle requests using object-oriented principles such as inheritance and mixins. They promote reusable and organized view logic, making it easier to manage complex views and avoid code duplication.

**Pros of CBVs:

**Cons of CBVs:

**Example: Creating a class-based view

Python `

from django.shortcuts import render, redirect from django.urls import reverse from django.views import View from .forms import MyForm

class MyCreateView(View): template_name = 'form.html' form_class = MyForm

def get(self, request, *args, **kwargs):
    form = self.form_class()
    return render(request, self.template_name, {'form': form})

def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST)
    if form.is_valid():
        form.save()
        return redirect(reverse('list-view'))
    return render(request, self.template_name, {'form': form})

`

**Note: Ensure that the template file 'form.html' exists inside your app's templates directory (e.g., templates/form.html), otherwise Django will raise a TemplateDoesNotExist error.

You can override class attributes dynamically when defining URL patterns:

Python `

from django.urls import path from .views import MyCreateView

urlpatterns = [ path('new/', MyCreateView.as_view(), name='original-create-view'), path( 'new_two/', MyCreateView.as_view( template_name='other_form.html', form_class=MyOtherForm ), name='modified-create-view' ), ]

`

Generic Class-Based View

Django’s generic CBVs provide pre-built views for common tasks like creating, updating, listing and deleting objects. They reduce boilerplate by automatically handling standard operations such as form display, validation, and saving.

Python `

from django.views.generic import CreateView from .models import MyModel from .forms import MyModelForm

class MyCreateView(CreateView): model = MyModel form_class = MyModelForm template_name = 'form.html' success_url = '/success/'

`

**In this example:

FBVs vs CBVs

Function-Based Views (FBVs) Class-Based Views (CBVs)
Best for simple or one-off views with minimal logic. Ideal for reusable or complex views.
Suitable for small, straightforward functionality. Handles multiple HTTP methods or advanced logic cleanly.
Quick to prototype with minimal overhead. Saves time with built-in generic views like CreateView, UpdateView, and DeleteView.
Provides full control over request handling. Promotes reusable code using inheritance and mixins.
Example Use Cases: Contact form, simple data display, basic API endpoint Example Use Cases: Blog post CRUD, user management, e-commerce product views