Chapter 4. Create CRUD Web Application

Django Generic Views

Django Generic Views
Tag:

Generic views in Django are built-in class-based views. Generic views are developed to eliminate repetitive coding by providing frequently used logic in class format. Using generic views, you can build web applications with simple code.

Different types of generic views are used depending on the objectives. In Chapter 2, we explained TemplateView, which is categorized under Base views. Base views also include View, which is the ancestor of all other class-based views, and RedirectView, which is used for URL redirections.

In a simple CRUD application, there are five frequently used generic views.

  1. ListView
  2. DetailView
  3. CreateView
  4. UpdateView
  5. DeleteView

They are categorized into two groups depending on their purposes – generic display views and generic editing views.

Generic display views

Generic display views are designed to display data. There are two views under this category – ListView and DetailView.

ListView

ListView is used to provide logic to display a list of objects. For example, ListView retrieves a list of blog articles from a database in the blog application.

DetailView

DetailView is used to provide logic to display a particular object. For example, the blog application retrieves a particular blog article from a database.

Generic editing views

Generic editing views are designed to provide a foundation for editing content. There are four views under this category – FormView, CreateView, UpdateView, and DeleteView. In this Chapter, we'll cover the following three views.

CreateView

CreateView is used to provide logic to display an input form to create an object. When inputs are submitted, CreateView handles the inputs and registers the data in a database.

UpdateView

UpdateView is used to provide logic to display an input form to update an object. When inputs are submitted, UpdateView handles the modification of the registered data in a database.

DeleteView

DeleteView is used to provide logic to display a page for the data delete action and handle the data delete process.

Generic views in Django are built-in class-based views. Generic views are developed to eliminate repetitive coding by providing frequently used logic in class format. Using generic views, you can build web applications with simple code.

Different types of generic views are used depending on the objectives. In Chapter 2, we explained TemplateView, which is categorized under Base views. Base views also include View, which is the ancestor of all other class-based views, and RedirectView, which is used for URL redirections.

In a simple CRUD application, there are five frequently used generic views.

  1. ListView
  2. DetailView
  3. CreateView
  4. UpdateView
  5. DeleteView

They are categorized into two groups depending on their purposes – generic display views and generic editing views.

Generic display views

Generic display views are designed to display data. There are two views under this category – ListView and DetailView.

ListView

ListView is used to provide logic to display a list of objects. For example, ListView retrieves a list of blog articles from a database in the blog application.

DetailView

DetailView is used to provide logic to display a particular object. For example, the blog application retrieves a particular blog article from a database.

Generic editing views

Generic editing views are designed to provide a foundation for editing content. There are four views under this category – FormView, CreateView, UpdateView, and DeleteView. In this Chapter, we'll cover the following three views.

CreateView

CreateView is used to provide logic to display an input form to create an object. When inputs are submitted, CreateView handles the inputs and registers the data in a database.

UpdateView

UpdateView is used to provide logic to display an input form to update an object. When inputs are submitted, UpdateView handles the modification of the registered data in a database.

DeleteView

DeleteView is used to provide logic to display a page for the data delete action and handle the data delete process.

Tag: