Skip to content

Commit

Permalink
Prevented the generic views from automatically creating a HEAD method…
Browse files Browse the repository at this point in the history
… 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
aaugustin committed Feb 18, 2012
1 parent 8d221e5 commit 52b06e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions django/views/generic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def as_view(cls, **initkwargs):

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
Expand Down Expand Up @@ -76,9 +78,6 @@ def http_method_not_allowed(self, request, *args, **kwargs):
)
return http.HttpResponseNotAllowed(allowed_methods)

def head(self, *args, **kwargs):
return self.get(*args, **kwargs)


class TemplateResponseMixin(object):
"""
Expand Down
15 changes: 14 additions & 1 deletion tests/regressiontests/generic_views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,12 +108,19 @@ def test_get_only(self):

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.
Expand Down

0 comments on commit 52b06e2

Please sign in to comment.