Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Prevented the generic views from automatically creating a HEAD method…

… when there is no GET. Reverts r16105, refs #17449.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17545 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 52b06e29c76dc9a5184edf3bbea64f0d4d5442b1 1 parent 8d221e5
Aymeric Augustin authored February 18, 2012
5  django/views/generic/base.py
@@ -43,6 +43,8 @@ def as_view(cls, **initkwargs):
43 43
 
44 44
         def view(request, *args, **kwargs):
45 45
             self = cls(**initkwargs)
  46
+            if hasattr(self, 'get') and not hasattr(self, 'head'):
  47
+                self.head = self.get
46 48
             return self.dispatch(request, *args, **kwargs)
47 49
 
48 50
         # take name and docstring from class
@@ -76,9 +78,6 @@ def http_method_not_allowed(self, request, *args, **kwargs):
76 78
         )
77 79
         return http.HttpResponseNotAllowed(allowed_methods)
78 80
 
79  
-    def head(self, *args, **kwargs):
80  
-        return self.get(*args, **kwargs)
81  
-
82 81
 
83 82
 class TemplateResponseMixin(object):
84 83
     """
15  tests/regressiontests/generic_views/base.py
@@ -19,9 +19,15 @@ class SimplePostView(SimpleView):
19 19
     post = SimpleView.get
20 20
 
21 21
 
  22
+class PostOnlyView(View):
  23
+    def post(self, request):
  24
+        return HttpResponse('This view only accepts POST')
  25
+
  26
+
22 27
 class CustomizableView(SimpleView):
23 28
     parameter = {}
24 29
 
  30
+
25 31
 def decorator(view):
26 32
     view.is_decorated = True
27 33
     return view
@@ -102,12 +108,19 @@ def test_get_only(self):
102 108
 
103 109
     def test_get_and_head(self):
104 110
         """
105  
-        Test a view which supplies a GET method also responds correctly to HEAD
  111
+        Test a view which supplies a GET method also responds correctly to HEAD.
106 112
         """
107 113
         self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
108 114
         response = SimpleView.as_view()(self.rf.head('/'))
109 115
         self.assertEqual(response.status_code, 200)
110 116
 
  117
+    def test_head_no_get(self):
  118
+        """
  119
+        Test a view which supplies no GET method responds to HEAD with HTTP 405.
  120
+        """
  121
+        response = PostOnlyView.as_view()(self.rf.head('/'))
  122
+        self.assertEqual(response.status_code, 405)
  123
+
111 124
     def test_get_and_post(self):
112 125
         """
113 126
         Test a view which only allows both GET and POST.

0 notes on commit 52b06e2

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