liveBook · Manning

Class-based view

As explained in Django in Action

A class-based view is a Django view implemented as a Python class whose methods handle requests and produce responses.

A class-based view (CBV) implements a Django view with a Python class rather than a function. Django supplies reusable view classes, and you can use them directly or inherit from them to define your own. section 14.1

The practical appeal is organization: when a view handles several HTTP methods, one class method can represent each action. That can reduce code, although the approach is more abstract than a function-based view. section 14.1

Use a CBV when:

  • You want Django-provided classes to supply reusable view behavior. section 14.1
  • You prefer grouping several HTTP-method actions inside one class. section 14.1
  • You are willing to trade some function-level simplicity for framework conventions and reuse. section 14.1

Django Admin pages

CBVs can add pages to the Django Admin navigation, such as an administrator-only report. Related admin extensions can also support specialized filters, links, buttons, wizards, and bulk operations. section 14.4

How Django in Action covers it

How deep it goes
Defines direct use or inheritance of Django classes, but stops short of implementation details and worked code.
Examples
Uses CBVs for Django Admin navigation pages, including administrator-only reports.
What it assumes
Builds on function-based views and HTTP methods while contrasting class-based organization with functions.
What you can do after
Apply via Django by using a provided class or inheriting from one to define a view.
How it connects
Connects class-based views with function-based views, HTTP methods, and Django Admin customization.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.