Class-based views

    Django provides base view classes which will suit a wide range of applications.All views inherit from the class, whichhandles linking the view in to the URLs, HTTP method dispatching and othercommon features. RedirectView provides aHTTP redirect, and extends thebase class to make it also render a template.

    The most direct way to use generic views is to create them directly in yourURLconf. If you’re only changing a few attributes on a class-based view, youcan pass them into the as_view() methodcall itself:

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

    Then we need to add this new view into our URLconf.TemplateView is a class, not a function, sowe point the URL to the classmethod instead, which provides a function-like entry to class-based views:

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

    We map the URL to book list view in the URLconf:

    If the view is accessed from a GET request, an object list is returned inthe response (using the book_list.html template). But if the client issuesa HEAD request, the response has an empty body and the header indicates when the most recent book was published. Based on thisinformation, the client may or may not download the full object list.