Chapter 4. Create CRUD Web Application

How To Write Class-Based Views with Generic Views

How To Write Class-Based Views with Generic Views
Tag:

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.

  1. Import generic views and models used in this view
  2. Create a new view using a generic view
  3. Set 'attribute values' for each attribute defined for each generic view
  4. Customize the generic view (e.g., set a new context)

1 Import generic views and models used in this view

Import generic views

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.

Import models

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.

2 Create a new view using a generic view

As explained in Chapter 2, you can create a view through inheritance.

The way to write a class-based view follows Python class syntax.

  • Class name is usually written in PascalCase (Capitalize all words) like MyView in the main figure.
  • Use the parent class name as an argument of the class to inherit the parent class, like ListView in the main figure

3 Set 'attribute values' for each attribute defined for each generic view

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 view
  • model: set a model to be used for this view

4 Customize the generic view

Although 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.

  • add a query to show selected data or to change the order of content items
  • add context (a dictionary of data that is passed from a view to an HTML template)
  • add logic to display content such as differentiating user interface depending on login status

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.

  1. Import generic views and models used in this view
  2. Create a new view using a generic view
  3. Set 'attribute values' for each attribute defined for each generic view
  4. Customize the generic view (e.g., set a new context)

1 Import generic views and models used in this view

Import generic views

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.

Import models

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.

2 Create a new view using a generic view

As explained in Chapter 2, you can create a view through inheritance.

The way to write a class-based view follows Python class syntax.

  • Class name is usually written in PascalCase (Capitalize all words) like MyView in the main figure.
  • Use the parent class name as an argument of the class to inherit the parent class, like ListView in the main figure

3 Set 'attribute values' for each attribute defined for each generic view

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 view
  • model: set a model to be used for this view

4 Customize the generic view

Although 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.

  • add a query to show selected data or to change the order of content items
  • add context (a dictionary of data that is passed from a view to an HTML template)
  • add logic to display content such as differentiating user interface depending on login status

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.

Tag: