Django generic views are designed using Python classes. How to use generic views in views.py follows the Python class syntax. Basically, you'll need to create a new view as a child class of a built-in generic view. The new view inherits the attributes and methods from the built-in generic view. When you make a new view using a generic view, the code has four sections.
First, you need to import the generic views you want to use. Generic views are stored in django.views.generic
. In the example in the main figure, ListView is imported.
You also need to import models you want to use in your view. The .
(dot) means that the same directory as the views.py is located. In the example in the main figure, MyModel is imported.
As explained in Chapter 2, you can create a view through inheritance.
The way to write a class-based view follows Python class syntax.
Each generic view has its own attributes. As generic views inherit attributes from their ancestor views, several common attributes are used for different generic views. There are two attributes in the main figure example. There are many other attributes. We'll explain the key ones in the next section.
template_name
: set an html file used for this viewmodel
: set a model to be used for this viewAlthough generic views are equipped with basic functionalities, you may need to customize them. For example, you can do the following things by customizing generic views.
Customization of views is done in a way that overrides the method defined in the generic view. In the main figure example, get_context_data
is a method to get the context data of the model. The get_context_data
method is accessible from ListView, but it is initially defined in the SingleObjectMixin class. ListView inherits the method from the SingleObjectMixin class.
We'll explain the context and how to override a method later in this chapter.
Django generic views are designed using Python classes. How to use generic views in views.py follows the Python class syntax. Basically, you'll need to create a new view as a child class of a built-in generic view. The new view inherits the attributes and methods from the built-in generic view. When you make a new view using a generic view, the code has four sections.
First, you need to import the generic views you want to use. Generic views are stored in django.views.generic
. In the example in the main figure, ListView is imported.
You also need to import models you want to use in your view. The .
(dot) means that the same directory as the views.py is located. In the example in the main figure, MyModel is imported.
As explained in Chapter 2, you can create a view through inheritance.
The way to write a class-based view follows Python class syntax.
Each generic view has its own attributes. As generic views inherit attributes from their ancestor views, several common attributes are used for different generic views. There are two attributes in the main figure example. There are many other attributes. We'll explain the key ones in the next section.
template_name
: set an html file used for this viewmodel
: set a model to be used for this viewAlthough generic views are equipped with basic functionalities, you may need to customize them. For example, you can do the following things by customizing generic views.
Customization of views is done in a way that overrides the method defined in the generic view. In the main figure example, get_context_data
is a method to get the context data of the model. The get_context_data
method is accessible from ListView, but it is initially defined in the SingleObjectMixin class. ListView inherits the method from the SingleObjectMixin class.
We'll explain the context and how to override a method later in this chapter.