A view is a callable which takes a request and returns a
response. This can be more than just a function, and Django provides
an example of some classes which can be used as views. These allow you
to structure your views and reuse code by harnessing inheritance and
mixins. There are also some generic views for simple tasks which we’ll
get to later, but you may want to design your own structure of
reusable views which suits your use case. For full details, see the
class-based views reference documentation.

Django provides base view classes which will suit a wide range of applications.
All views inherit from the View class, which
handles linking the view in to the URLs, HTTP method dispatching and other
simple features. RedirectView is for a
simple HTTP redirect, and TemplateView
extends the base class to make it also render a template.

The simplest way to use generic views is to create them directly in your
URLconf. If you’re only changing a few simple attributes on a class-based view,
you can simply pass them into the
as_view() method call itself:

Any arguments passed to as_view() will
override attributes set on the class. In this example, we set template_name
on the TemplateView. A similar overriding pattern can be used for the
url attribute on RedirectView.

The second, more powerful way to use generic views is to inherit from an
existing view and override attributes (such as the template_name) or
methods (such as get_context_data) in your subclass to provide new values
or methods. Consider, for example, a view that just displays one template,
about.html. Django has a generic view to do this -
TemplateView - so we can just subclass it,
and override the template name:

Then we just need to add this new view into our URLconf.
TemplateView is a class, not a function,
so we point the URL to the as_view()
class method instead, which provides a function-like entry to class-based
views:

Suppose somebody wants to access our book library over HTTP using the views
as an API. The API client would connect every now and then and download book
data for the books published since last visit. But if no new books appeared
since then, it is a waste of CPU time and bandwidth to fetch the books from the
database, render a full response and send it to the client. It might be
preferable to ask the API when the most recent book was published.

If the view is accessed from a GET request, a plain-and-simple object
list is returned in the response (using book_list.html template). But if
the client issues a HEAD request, the response has an empty body and
the Last-Modified header indicates when the most recent book was published.
Based on this information, the client may or may not download the full object
list.

This document is for Django's development version, which can be significantly different from previous releases. For older releases, use the version selector floating in the bottom right corner of this page.