Class-based generic views

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

View

This is the parent of all views. It performs dispatch and sanity checks.

Base

TemplateView

This renders a template. It exposes the URLConf keywords into context.

Base

RedirectView

This redirects on any GET request.

List

ListView

This renders any iterable of items, such as a queryset.

Detail

DetailView

This renders an item based on pk or slug from URLConf.

Edit

FormView

This renders and processes a form.

Edit

CreateView

This renders and processes a form for creating new objects.

Edit

UpdateView

This renders and processes a form for updating an object.

Edit

DeleteView

This renders and processes a form for deleting an object.

Date

ArchiveIndexView

This renders a list of objects with a date field, the latest being the first.

Date

YearArchiveView

This renders a list of objects on year given by URLConf.

Date

MonthArchiveView

This renders a list of objects on a year and month.

Date

WeekArchiveView

This renders a list of objects on a year and week number.

Date

DayArchiveView

This renders a list of objects on a year, month, and day.

Date

TodayArchiveView

This renders a list of objects on today's date.

Date

DateDetailView

This renders an object on a year, month, and day identified by its pk or slug.

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:

  • The only generic views are the ones bundled with Django: Thankfully, this is wrong. There is no special magic in the generic class-based views that are provided.

    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.

  • Class-based views must always derive from a generic view: Again, there is nothing magical about the generic view classes. Though 90 percent of the time, you will find a generic class such as View to be ideal for use as a base class, you are free to implement similar features yourself.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset