Django Views: Function-Based vs Class-Based (When to Use What)

Shriful-Islam
Shriful Islam
Published on Jan, 23 2026 2 min read 0 comments
image

Introduction

When building a Django application, views are the heart of your web logic.
They determine what users see when they visit a URL and how data is processed.

Django gives you two main approaches to writing views:

  1. Function-Based Views (FBV)
  2. Class-Based Views (CBV)

In this article, we’ll explore:

  • Differences between FBV and CBV
  • Pros and cons of each
  • Real-world examples for when to use them

By the end, you’ll know exactly which view type to pick for your Django projects.

What Are Function-Based Views (FBV)?

A function-based view is simply a Python function that:

  • Receives a request
  • Processes data
  • Returns a response

Example:

from django.shortcuts import render
from .models import BlogPost

def home(request):
    posts = BlogPost.objects.all()
    return render(request, 'blog/home.html', {'posts': posts})

Pros:

  • Simple and easy to read
  • Explicit logic
  • Great for small projects or simple pages

Cons:

  • Can become repetitive for common operations (CRUD)
  • Harder to scale for complex apps

What Are Class-Based Views (CBV)?

A class-based view is a Python class that inherits from Django’s generic views or View class.

Example:

from django.views.generic import ListView
from .models import BlogPost

class HomeView(ListView):
    model = BlogPost
    template_name = 'blog/home.html'
    context_object_name = 'posts'

Pros:

  • Reusable and modular
  • Built-in generic views for CRUD: ListView, DetailView, CreateView, UpdateView, DeleteView
  • Easier to extend with mixins

Cons:

  • Slightly steeper learning curve
  • Less explicit than FBVs

When to Use FBV vs CBV

| Criteria       | Function-Based View (FBV) | Class-Based View (CBV)    |
| -------------- | ------------------------- | ------------------------- |
| Simplicity     | ✅ Easy to understand      | ❌ Slightly more complex   |
| CRUD           | ❌ Repetitive              | ✅ Generic views handle it |
| Reusability    | ❌ Limited                 | ✅ High, via mixins        |
| Extensibility  | ❌ Harder                  | ✅ Easy to extend          |
| Learning Curve | ✅ Beginner-friendly       | ❌ Needs practice          |

Rule of Thumb:

  • FBV: Quick pages, simple logic, or one-off views
  • CBV: Scalable apps, CRUD operations, APIs, complex websites

Real-Life Example: Blog CRUD

Function-Based Views

# views.py
def post_detail(request, pk):
    post = BlogPost.objects.get(pk=pk)
    return render(request, 'blog/detail.html', {'post': post})

Class-Based Views

from django.views.generic import DetailView

class PostDetailView(DetailView):
    model = BlogPost
    template_name = 'blog/detail.html'
    context_object_name = 'post'

Observation:

  • CBVs reduce boilerplate
  • FBVs give full control

Mixing FBV & CBV in the Same Project

It’s perfectly fine to mix both:

  • Use FBV for small, custom pages
  • Use CBV for CRUD-heavy apps

Example project:

/blog
    views.py       # FBVs for landing pages
    cbv_views.py   # CBVs for post CRUD

Tips for Choosing the Right Approach

  1. Start with FBV for small apps
  2. Move to CBV when the logic repeats across models
  3. Use mixins for reusable behavior in CBVs
  4. Keep readability as your first priority

Final Thoughts

Understanding FBVs vs CBVs is essential to writing clean and scalable Django code.

  • FBVs → Explicit, simple, beginner-friendly
  • CBVs → Reusable, modular, professional-grade

By choosing the right approach, you’ll save time, reduce code duplication, and make your projects easier to maintain.

 

0 Comments