From Function Views to View Classes
Why CBVs exist and when to use them.
From Function Views to View Classes is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Build Views
So far your views were plain functions. Django also lets you write views as classes, which package related logic together neatly.
What a Class-Based View Is
A class-based view (CBV) is just a Python class that handles a request. Django turns it into a callable for you behind the scenes.
The Repetition Problem
Function views often repeat the same shape: fetch data, build context, render a template. CBVs let you reuse that shape instead of copying it.
A Function View, For Contrast
Here is a familiar function view. It takes a request and returns a response, all in one block.
def home(request):
return render(request, "home.html")The Same View as a Class
As a class, you subclass View and define a method per HTTP verb. The logic is the same, just organized differently.
class HomeView(View):
def get(self, request):
return render(request, "home.html")Methods Map to HTTP Verbs
In a CBV, a get method handles GET requests and a post method handles POST. No more giant if-checks on request.method.
Inheritance Means Reuse
Because CBVs are classes, you can subclass and override only what changes. Shared behavior lives in a parent class once.
Django Ships Generic Views
Django includes ready-made generic views like ListView and DetailView. They cover common patterns so you write almost no code.
When Functions Still Win
For a tiny, one-off view, a function is often clearer. Reach for CBVs when logic repeats or grows complex.
Both Plug Into urls.py
Both styles get wired the same way in urls. A CBV just needs as_view() to become a callable Django can route to.
path("", HomeView.as_view())Pick the Right Tool
CBVs are not better or worse, just different. Choose based on reuse: repeated patterns favor classes, simple pages favor functions.
Quick Check
What is the main reason class-based views exist?
Recap: Functions vs Classes
You met class-based views: classes that handle requests, map methods to HTTP verbs, and reuse logic through inheritance. Great when patterns repeat. 🎯
Frequently asked questions
Is the “From Function Views to View Classes” lesson free?
Yes — the full text of “From Function Views to View Classes” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “From Function Views to View Classes”?
Why CBVs exist and when to use them. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “From Function Views to View Classes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- From Function Views to View Classes
- TemplateView and get_context_data
- Handling get and post Methods
- Mixins and as_view()