Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15688 - Generic views should provide a HEAD implementation

Thanks to j4mie for the report and patch!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16095 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f4c808b7f50d6906d96593626ccdd016b0b1b96b 1 parent 065e6b6
Luke Plant authored April 23, 2011
2  django/views/generic/base.py
@@ -44,6 +44,8 @@ def as_view(cls, **initkwargs):
44 44
 
45 45
         def view(request, *args, **kwargs):
46 46
             self = cls(**initkwargs)
  47
+            if hasattr(self, 'get') and not hasattr(self, 'head'):
  48
+                self.head = self.get
47 49
             return self.dispatch(request, *args, **kwargs)
48 50
 
49 51
         # take name and docstring from class
15  tests/regressiontests/generic_views/base.py
@@ -101,6 +101,14 @@ def test_get_only(self):
101 101
             self.rf.get('/', REQUEST_METHOD='FAKE')
102 102
         ).status_code, 405)
103 103
 
  104
+    def test_get_and_head(self):
  105
+        """
  106
+        Test a view which supplies a GET method also responds correctly to HEAD
  107
+        """
  108
+        self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
  109
+        response = SimpleView.as_view()(self.rf.head('/'))
  110
+        self.assertEqual(response.status_code, 200)
  111
+
104 112
     def test_get_and_post(self):
105 113
         """
106 114
         Test a view which only allows both GET and POST.
@@ -167,6 +175,13 @@ def test_get(self):
167 175
         """
168 176
         self._assert_about(AboutTemplateView.as_view()(self.rf.get('/about/')))
169 177
 
  178
+    def test_head(self):
  179
+        """
  180
+        Test a TemplateView responds correctly to HEAD
  181
+        """
  182
+        response = AboutTemplateView.as_view()(self.rf.head('/about/'))
  183
+        self.assertEqual(response.status_code, 200)
  184
+
170 185
     def test_get_template_attribute(self):
171 186
         """
172 187
         Test a view that renders a template on GET with the template name as

0 notes on commit f4c808b

Please sign in to comment.
Something went wrong with that request. Please try again.