• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Class-Based Views (CBVs) are Django’s object-oriented view pattern where views are Python classes inheriting from View or generic view classes (TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView), with behavior defined by overriding methods (get, post, get_queryset, get_context_data) and composed via mixins.

Explanation

CBVs solve the problem of repetitive boilerplate in common view patterns (list, detail, create, update, delete) by providing reusable base classes that implement standard CRUD workflows. Instead of writing if request.method == 'POST': in every function, developers override specific methods (form_valid, get_queryset) and the framework handles the rest. Mixins (LoginRequiredMixin, PermissionRequiredMixin) compose cross-cutting concerns declaratively.

How It Works

  1. URL maps to as_view()path('post/<int:pk>/', PostDetailView.as_view(), name='detail')
  2. as_view() returns callable — Factory function that instantiates view class per request
  3. dispatch() routes by method — Calls get(), post(), put(), delete(), etc.
  4. Generic views provide defaultsListView paginates get_queryset(); CreateView handles form GET/POST
  5. Method overrides customizeget_queryset(), get_context_data(), form_valid(), get_success_url()
  6. Mixins inject behaviorLoginRequiredMixin adds dispatch check; SuccessMessageMixin adds messages

Visual Explanation

class_based_views URLConf path('post/<int:pk>/',  PostDetailView.as_view()) AsView PostDetailView.as_view() → view_func(request) URLConf->AsView 1. Resolve Dispatch dispatch(request) → self.get(request) AsView->Dispatch 2. Call GetMethod get(request) → self.get_object() → self.get_context_data() → render() Dispatch->GetMethod 3. Route GET GetObject get_object() → get_queryset().get(pk=...) GetMethod->GetObject 4. Fetch object ContextData get_context_data() → {'object': post} GetMethod->ContextData 5. Build context Template template_name 'post_detail.html' ContextData->Template 6. Render

Semantic Network

semantic_class_based_views THIS Class-Based Views (CBV) PRE1 URL Dispatcher THIS--PRE1 built from PRE2 View Base Class THIS--PRE2 built from PRE3 Mixin Classes THIS--PRE3 built from OUT1 Generic Display Views THIS--OUT1 builds into OUT2 Generic Editing Views THIS--OUT2 builds into OUT3 Method Overrides THIS--OUT3 builds into OUT4 Mixin Composition THIS--OUT4 builds into CON1 Function-Based Views (FBV) THIS--CON1 contrasts with REL1 Template Rendering THIS--REL1 related REL2 Form Handling THIS--REL2 related

Key Properties

  • Base View class: Implements dispatch(), http_method_not_allowed(), options()
  • Generic display views: TemplateView (render template), ListView (paginated list), DetailView (single object)
  • Generic editing views: FormView (form handling), CreateView, UpdateView, DeleteView (model CRUD)
  • Mixin pattern: LoginRequiredMixin, PermissionRequiredMixin, UserPassesTestMixin, SuccessMessageMixin
  • MRO matters: Mixin order affects method resolution; LoginRequiredMixin before View for auth check

Connections

Edge Cases & Gotchas

  • get_object() 404: DetailView calls get_object() which raises 404; override for custom lookup
  • success_url vs get_success_url(): Static string vs dynamic (e.g., reverse_lazy or object method)
  • MRO conflicts: Multiple mixins overriding same method — order in class declaration matters
  • context_object_name: ListView uses object_list; DetailView uses object; customize for clarity
  • Form kwargs: CreateView/UpdateView pass instance to form; get_form_kwargs() for extra data