Class-based generic views are commonly used views implemented in an object-oriented manner (Template method pattern) for better reuse. I hate the term generic views. I would rather call them stock views. Like stock photographs, you can use them for many common needs with a bit of tweaking.
Generic views were created because Django developers felt that they were recreating the same kind of views in every project. Nearly every project needed a page showing a list of objects (ListView
), details of an object (DetailView
), or a form to create an object (CreateView
). In the spirit of DRY, these reusable views were bundled with Django.
A convenient table of generic views in Django 1.7 is given here:
Type |
Class Name |
Description |
---|---|---|
Base |
|
This is the parent of all views. It performs dispatch and sanity checks. |
Base |
|
This renders a template. It exposes the |
Base |
|
This redirects on any |
List |
|
This renders any iterable of items, such as a |
Detail |
|
This renders an item based on |
Edit |
|
This renders and processes a form. |
Edit |
|
This renders and processes a form for creating new objects. |
Edit |
|
This renders and processes a form for updating an object. |
Edit |
|
This renders and processes a form for deleting an object. |
Date |
|
This renders a list of objects with a date field, the latest being the first. |
Date |
|
This renders a list of objects on |
Date |
|
This renders a list of objects on a |
Date |
|
This renders a list of objects on a |
Date |
|
This renders a list of objects on a |
Date |
|
This renders a list of objects on today's date. |
Date |
|
This renders an object on a |
We have not mentioned base classes such as BaseDetailView
or mixins such as SingleObjectMixin
here. They are designed to be parent classes. In most cases, you would not use them directly.
Most people confuse class-based views and class-based generic views. Their names are similar but they are not the same things. This has led to some interesting misconceptions as follows:
You are free to roll your own set of generic class-based views. You can also use a third-party library such as django-vanilla-views
(http://django-vanilla-views.org/), which has a simpler implementation of the standard generic views. Remember that using custom generic views might make your code unfamiliar to others.
View
to be ideal for use as a base class, you are free to implement similar features yourself.