Ticket #15906: patch_15906.diff

File patch_15906.diff, 2.9 KB (added by zsiciarz, 4 years ago)

A new section on how to use head() in a generic view.

  • docs/ref/class-based-views.txt

    diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
    index 692417e..5ab948e 100644
    a b View 
    867867        delegated to :meth:`~View.get()`, a ``POST`` to :meth:`~View.post()`,
    868868        and so on.
    869869
     870        By default, a ``HEAD`` request will be delegated to :meth:`~View.get()`.
     871        If you need to handle ``HEAD`` requests in a different way than ``GET``,
     872        you can override the :meth:`~View.head()` method.
     873        See :ref:`supporting-other-http-methods` for an example.
     874
    870875        The default implementation also sets ``request``, ``args`` and
    871876        ``kwargs`` as instance variables, so any method on the view can know
    872877        the full details of the request that was made to invoke the view.
  • docs/topics/class-based-views.txt

    diff --git a/docs/topics/class-based-views.txt b/docs/topics/class-based-views.txt
    index 3812fae..1496e63 100644
    a b Because of the way that Python resolves method overloading, the local 
    554554:class:`JSONResponseMixin` and
    555555:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`.
    556556
     557.. _supporting-other-http-methods:
     558
     559Supporting other HTTP methods
     560-----------------------------
     561
     562Suppose somebody wants to access our book library over HTTP using the views
     563as an API. The API client would connect every now and then and download book
     564data for the books published since last visit. But if no new books appeared
     565since then, it is a waste of CPU time and bandwidth to fetch the books from
     566database, render a full response and send it to the client. It might be
     567preferable to ask the API when the most recent book was published.
     568
     569We define the URL to book list in the URLconf::
     570
     571    from django.conf.urls import patterns
     572    from books.views import BookListView
     573
     574    urlpatterns = patterns('',
     575        (r'^books/$', BookListView.as_view()),
     576    )
     577
     578And the view::
     579
     580    from django.http import HttpResponse
     581    from django.views.generic import ListView
     582    from books.models import Book
     583
     584    class BookListView(ListView):
     585
     586        model = Book
     587
     588        def head(self, *args, **kwargs):
     589            last_book = self.get_queryset().latest('publication_date')
     590            response = HttpResponse('')
     591            # RFC 1123 date format
     592            response['Last-Modified'] = last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT')
     593            return response
     594
     595If the view is accessed from a ``GET`` request, a plain-and-simple object
     596list is returned in the response (using ``book_list.html`` template). But if
     597the client issues a ``HEAD`` request, the response has an empty body and
     598the ``Last-Modified`` header indicates when the most recent book was published.
     599Based on this information, the client may or may not download the full object
     600list.
     601
    557602Decorating class-based views
    558603============================
    559604
Back to Top