What is the difference between a function-based view and a class-based view in Django?
Compare Django function-based views and class-based views: syntax, generic views, mixins, trade-offs and when to choose each, with code examples.
Expected Interview Answer
A function-based view (FBV) is a plain Python function that takes a request and returns a response, while a class-based view (CBV) is a class whose methods handle each HTTP verb, letting you reuse behaviour through inheritance and mixins.
FBVs are explicit and easy to read: you branch on request.method yourself, so all logic sits in one place. CBVs organize logic by HTTP method (get(), post()) and provide generic base classes like ListView, DetailView and CreateView that handle common CRUD patterns with minimal code. CBVs shine for repetitive, standard behaviour and composition via mixins, but their inheritance chain can be harder to trace; FBVs stay clearer for one-off or heavily custom logic.
- FBVs are explicit and quick to read for simple logic
- CBVs reduce boilerplate for standard CRUD via generics
- CBVs promote reuse through inheritance and mixins
- CBVs organize code cleanly by HTTP method
- You can mix both approaches in the same project
AI Mentor Explanation
A function-based view is like a specialist all-rounder who does everything in one over — you tell him exactly what to bowl and he handles each ball himself. A class-based view is like a coaching template a whole squad inherits: the batting drill is defined once and every player refines only the shots they need. CBVs give shared structure through inheritance, FBVs give hands-on, ball-by-ball control.
Step-by-Step Explanation
Step 1
Write an FBV
Define a function taking request, branch on request.method, and return an HttpResponse or render().
Step 2
Write a CBV
Subclass View (or a generic) and implement get(), post() etc.; wire it with as_view() in urls.py.
Step 3
Use generic CBVs
Reach for ListView, DetailView, CreateView, UpdateView to cover common CRUD with little code.
Step 4
Compose with mixins
Add mixins like LoginRequiredMixin to layer reusable behaviour onto a CBV.
Step 5
Choose per case
Pick FBVs for simple or custom logic and CBVs for repetitive, standard patterns.
What Interviewer Expects
- Clear definition of both FBV and CBV
- Knows CBVs dispatch by HTTP method via as_view()
- Familiar with generic views like ListView and CreateView
- Understands mixins and inheritance for reuse
- Can articulate trade-offs and when to choose each
Common Mistakes
- Claiming CBVs are always better than FBVs
- Forgetting as_view() when wiring a CBV in urls.py
- Not knowing which method (get/post) handles which verb
- Overusing deep inheritance chains that are hard to trace
- Thinking FBVs cannot handle multiple HTTP methods
Best Answer (HR Friendly)
“In Django, a function-based view is a single function that handles a web request, which is simple and direct. A class-based view is a class that can reuse common patterns through inheritance, saving code when the same behaviour repeats across pages.”
Code Example
# Function-based view
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'post_detail.html', {'post': post})
# Class-based view (generic)
from django.views.generic import DetailView
class PostDetailView(DetailView):
model = Post
template_name = 'post_detail.html'
# urls.py
# path('<int:pk>/', PostDetailView.as_view(), name='detail')Follow-up Questions
- When would you prefer an FBV over a CBV?
- What does as_view() do under the hood?
- Name three generic class-based views and their use.
- How do mixins add reusable behaviour to CBVs?
- How does a CBV route a POST request to the right method?
MCQ Practice
1. How is a class-based view wired into urls.py?
as_view() returns a callable that Django's URL resolver can dispatch to, mapping HTTP verbs to methods.
2. Which generic CBV is best for displaying a list of objects?
ListView renders a queryset as a list with minimal code, handling pagination and context automatically.
3. In an FBV, how do you handle different HTTP methods?
An FBV inspects request.method and branches, whereas a CBV separates them into get(), post() methods.
Flash Cards
What is an FBV? — A plain function taking request and returning a response; you branch on request.method yourself.
What is a CBV? — A class whose methods (get, post) handle HTTP verbs, enabling reuse via inheritance and mixins.
What wires a CBV to a URL? — ClassName.as_view() returns the callable used in urlpatterns.
When to prefer a CBV? — For repetitive, standard CRUD patterns handled by generics like ListView and CreateView.