Code

Ticket #15906: patch_15906.diff

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

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

Line 
1diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
2index 692417e..5ab948e 100644
3--- a/docs/ref/class-based-views.txt
4+++ b/docs/ref/class-based-views.txt
5@@ -867,6 +867,11 @@ View
6         delegated to :meth:`~View.get()`, a ``POST`` to :meth:`~View.post()`,
7         and so on.
8 
9+        By default, a ``HEAD`` request will be delegated to :meth:`~View.get()`.
10+        If you need to handle ``HEAD`` requests in a different way than ``GET``,
11+        you can override the :meth:`~View.head()` method.
12+        See :ref:`supporting-other-http-methods` for an example.
13+
14         The default implementation also sets ``request``, ``args`` and
15         ``kwargs`` as instance variables, so any method on the view can know
16         the full details of the request that was made to invoke the view.
17diff --git a/docs/topics/class-based-views.txt b/docs/topics/class-based-views.txt
18index 3812fae..1496e63 100644
19--- a/docs/topics/class-based-views.txt
20+++ b/docs/topics/class-based-views.txt
21@@ -554,6 +554,51 @@ Because of the way that Python resolves method overloading, the local
22 :class:`JSONResponseMixin` and
23 :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`.
24 
25+.. _supporting-other-http-methods:
26+
27+Supporting other HTTP methods
28+-----------------------------
29+
30+Suppose somebody wants to access our book library over HTTP using the views
31+as an API. The API client would connect every now and then and download book
32+data for the books published since last visit. But if no new books appeared
33+since then, it is a waste of CPU time and bandwidth to fetch the books from
34+database, render a full response and send it to the client. It might be
35+preferable to ask the API when the most recent book was published.
36+
37+We define the URL to book list in the URLconf::
38+
39+    from django.conf.urls import patterns
40+    from books.views import BookListView
41+
42+    urlpatterns = patterns('',
43+        (r'^books/$', BookListView.as_view()),
44+    )
45+
46+And the view::
47+
48+    from django.http import HttpResponse
49+    from django.views.generic import ListView
50+    from books.models import Book
51+
52+    class BookListView(ListView):
53+
54+        model = Book
55+
56+        def head(self, *args, **kwargs):
57+            last_book = self.get_queryset().latest('publication_date')
58+            response = HttpResponse('')
59+            # RFC 1123 date format
60+            response['Last-Modified'] = last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT')
61+            return response
62+
63+If the view is accessed from a ``GET`` request, a plain-and-simple object
64+list is returned in the response (using ``book_list.html`` template). But if
65+the client issues a ``HEAD`` request, the response has an empty body and
66+the ``Last-Modified`` header indicates when the most recent book was published.
67+Based on this information, the client may or may not download the full object
68+list.
69+
70 Decorating class-based views
71 ============================
72