Class-based view is a new view writing method introduced by Django 1.3. It is used to replace the previous function-based view writing method.
With the convenience of multiple inheritance features in OO and python, class-based views can provide better abstraction and reuse capabilities. The new general view will be more elegant.
Django has many documents, but it is often not enough in actual development. In many cases, you still need to go deep into the source code. To this end, we carefully reviewed the implementation of class-based views. We hope to provide a clearer and direct reference for future development.
Description: Django applies multiple inheritance features in a large number. To split some common functional code into a class (Mixin), you only need to add the mixed class to the class inheritance list when you need the code that is mixed into the class-pay attention to the order, the class on the left has higher permissions and will overwrite the functions with the same name in the inherited class on the right. For ease of instruction, you can turn these mixed classes (Mixin) into "tool classes ".
Source Code Organization
All view-related code is stored in the directory Django/views/generic:
-
Base. py: the abstract class for saving views, templateview and redirectview, and the Code for templateresponsemixin.
-
Create_update.py is a function-based general view. MarkedDeprecated
-
Date_based.py is a function-based date-related general view. MarkedDeprecated
-
Dates. py is a new generic class-based date view. Used to replace date_based.py
-
Detail. py displays related views and tool classes for a single object based on the class
-
Edit. py: class-based object editing related views and tool classes
-
List. py displays related views and tool classes based on the class Object List
-
List_detail.py is a function-based list and display function. MarkedDeprecated
-
Simply. py is a common function-based tool. MarkedDeprecated
Logically, the source code is organized into abstract base classes, tool classes (Mixin), conventional (specific) implementations, and model-based implementations.
Django-class-based view source code analysis 1