This article is Day 13 of Django Advent Calendar 2019. In this article, I'd like to give you an overview of Django's Class-based Generic View and its inherited class relationships.
I refer to and cite the Official Documentation of Django 3.0, the latest version at the time of this article's creation.
First, a "view" in Django is an encapsulation of logic that processes a user request and returns a response [^ 1]. Define the data to be displayed in the "template" which is the presentation layer, and perform processing according to the request. It's arguable, but the "controller" in the MVC framework may be closer to the general image [^ 2].
Django provides a "Class-based Generic View," which generalizes the logic of developing that view. As the name suggests, class-based generic views are class-based views, so code can be reused using inheritance [^ 3]. By utilizing the general-purpose view, you can avoid redundancy and boiler templates in Web development, and develop by making the most of Django's design concept of "Less Code" and "Don't Repeat Yourself".
A list and overview of generic views. The importance is set based on the frequency of personal use in normal development. I think the importance depends on the characteristics of the service.
A group of base views that define the minimum functionality of a general-purpose view. The class here is also inherited from other generic views.
class | importance | Overview |
---|---|---|
View | ★★★ | Base view of generic view |
TemplateView | ★★★ | Base view of the view that renders the template |
RedirectView | ★★☆ | View for redirect |
A group of views for displaying the list / detail screen. I think this is the most frequent class in general website development.
class | importance | Overview |
---|---|---|
DetailView | ★★★ | View to display detail screen |
ListView | ★★★ | View for displaying the list screen |
A group of views for displaying the edit screen. This class can be used on general CRUD operation screens.
class | importance | Overview |
---|---|---|
FormView | ★★☆ | Form screen view |
CreateView | ★★☆ | View of creation screen |
UpdateView | ★★☆ | Update screen view |
DeleteView | ★★☆ | Delete screen view |
It is a group of views for displaying a list by year, month, etc. like a blog.
class | importance | Overview |
---|---|---|
ArchiveIndexView | ★☆☆ | View for listing items with the latest dates |
YearArchiveView | ★☆☆ | View for displaying the list screen by year |
MonthArchiveView | ★☆☆ | View for displaying the monthly list screen |
WeekArchiveView | ★☆☆ | View to display the list screen by week |
DayArchiveView | ★☆☆ | View for displaying the daily list screen |
TodayArchiveView | ★☆☆ | View for displaying the list screen of today's date |
DateDetailView | ★☆☆ | View for displaying the details screen for each date |
A generic view itself inherits another generic view, or is configured with multiple inheritances of a generic mixin. A generic view defines the behavior of a view by setting its attributes and behavior. Note that most attributes have accessors defined for them, so it is possible to dynamically change the value of an attribute by overriding the accessor.
Django.views.generic.base
)View
is the base class for all generic views, and View
has attributes referenced by other classes such as request`` kwargs
.
TemplateView
consists of multiple inheritance of TemplateResponseMixin
for rendering the template file and ContextMixin
which passes the context variable to the template. These two mixins are inherited by all generic views except RedirectView
.
Django.views.generic.list``
Django.views.generic.detail`)The main difference between ListView
and DetailView
is the Mixin surrounded by a red frame. You can see that the basic structure is almost the same, only the difference depends on whether the context variable passed to the template is a list or a single object.
Django.views.generic.edit
)Since there are many inherited classes, only the general view and mixin in the same module are surrounded by a red frame. All general-purpose views except DeleteView
inherit from FormMixin
which generates forms and ProcessFormView
which performs rendering at GET and validation at POST. For the remaining views and Mixins, the classes based on the CRUD operations associated with each editing view are inherited.
I think it's easy for people who are new to Django to get a feel for the generalization of generalized views of Django, understand classes and mixins and their attributes (fields), and behaviors (methods). There are many good articles on how-tos for class-based general-purpose views, so I tried to get a graphical overview of this article.
Recommended Posts