diff --git a/django/views/generic/base.py b/django/views/generic/base.py index f443cebfc0..6dfbc7a9da 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -43,6 +43,8 @@ class View(object): def view(request, *args, **kwargs): self = cls(**initkwargs) + if hasattr(self, 'get') and not hasattr(self, 'head'): + self.head = self.get return self.dispatch(request, *args, **kwargs) # take name and docstring from class @@ -76,9 +78,6 @@ class View(object): ) return http.HttpResponseNotAllowed(allowed_methods) - def head(self, *args, **kwargs): - return self.get(*args, **kwargs) - class TemplateResponseMixin(object): """ diff --git a/tests/regressiontests/generic_views/base.py b/tests/regressiontests/generic_views/base.py index d9debb6627..e7aeaf9cde 100644 --- a/tests/regressiontests/generic_views/base.py +++ b/tests/regressiontests/generic_views/base.py @@ -19,9 +19,15 @@ class SimplePostView(SimpleView): post = SimpleView.get +class PostOnlyView(View): + def post(self, request): + return HttpResponse('This view only accepts POST') + + class CustomizableView(SimpleView): parameter = {} + def decorator(view): view.is_decorated = True return view @@ -102,12 +108,19 @@ class ViewTest(unittest.TestCase): def test_get_and_head(self): """ - Test a view which supplies a GET method also responds correctly to HEAD + Test a view which supplies a GET method also responds correctly to HEAD. """ self._assert_simple(SimpleView.as_view()(self.rf.get('/'))) response = SimpleView.as_view()(self.rf.head('/')) self.assertEqual(response.status_code, 200) + def test_head_no_get(self): + """ + Test a view which supplies no GET method responds to HEAD with HTTP 405. + """ + response = PostOnlyView.as_view()(self.rf.head('/')) + self.assertEqual(response.status_code, 405) + def test_get_and_post(self): """ Test a view which only allows both GET and POST.