Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

class-based-views

Class-based views

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Class-based views: the syntax, APIs, and habits you need before advancing in Django.

Teams ship Class-based views on every Django codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Class-based views in contexts like: SaaS dashboards, CMS-style products, internal tools, and APIs paired with React or mobile clients.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Class-based views (CBVs) encapsulate HTTP handling in reusable classes. Django ships generics—ListView, DetailView, CreateView—that cut boilerplate for common patterns.

Generic list view

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

class ArticleListView(ListView):
    model = Article
    template_name = "blog/list.html"
    context_object_name = "articles"
    queryset = Article.objects.filter(published=True)

When to use CBVs

  • Standard CRUD with little custom logic—CreateView, UpdateView, DeleteView
  • Shared mixins (LoginRequiredMixin, PermissionRequiredMixin)
  • Prefer FBVs when flow is unique or readability beats abstraction

Important interview questions and answers

  1. Q: CBV vs FBV?
    A: CBVs reuse structure and mixins; FBVs are explicit—team preference and complexity decide.
  2. Q: as_view()?
    A: CBVs expose ArticleListView.as_view() to urls.py—Django instantiates per request.
  3. Q: Override get_queryset?
    A: Customize which rows the generic view fetches without rewriting dispatch logic.

Self-check

  1. Which generic view lists model instances?
  2. When is a function-based view clearer?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ListView queryset?
  • LoginRequired mixin?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump