Ticket #15668: 15668.patch

File 15668.patch, 3.0 KB (added by Jamie Matthews, 14 years ago)
  • django/views/generic/base.py

    diff --git a/django/views/generic/base.py b/django/views/generic/base.py
    index d732af5..760b1fe 100644
    a b class View(object):  
    4444
    4545        def view(request, *args, **kwargs):
    4646            self = cls(**initkwargs)
     47            if hasattr(self, 'get') and not hasattr(self, 'head'):
     48                self.head = self.get
    4749            return self.dispatch(request, *args, **kwargs)
    4850
    4951        # take name and docstring from class
  • tests/regressiontests/generic_views/base.py

    diff --git a/tests/regressiontests/generic_views/base.py b/tests/regressiontests/generic_views/base.py
    index e20932b..9a685ef 100644
    a b class InstanceView(View):  
    5656        return self
    5757
    5858
    59 class ViewTest(unittest.TestCase):
     59class ViewTest(TestCase):
     60    urls = 'regressiontests.generic_views.urls'
     61
    6062    rf = RequestFactory()
    6163
    6264    def _assert_simple(self, response):
    class ViewTest(unittest.TestCase):  
    101103            self.rf.get('/', REQUEST_METHOD='FAKE')
    102104        ).status_code, 405)
    103105
     106    def test_get_and_head(self):
     107        """
     108        Test a view which supplies a GET method also responds correctly to HEAD.
     109        Need to use TestClient rather than RequestFactory here because the content
     110        of the response is stripped by the conditional_content_removal function in
     111        django.http.utils, which is called by BaseHandler, so we need to invoke
     112        the complete request/response cycle.
     113        """
     114        response = self.client.head('/simple/')
     115        self.assertEqual(response.status_code, 200)
     116        self.assertEqual(response.content, '')
     117
    104118    def test_get_and_post(self):
    105119        """
    106120        Test a view which only allows both GET and POST.
    class TemplateViewTest(TestCase):  
    167181        """
    168182        self._assert_about(AboutTemplateView.as_view()(self.rf.get('/about/')))
    169183
     184    def test_head(self):
     185        """
     186        Test a TemplateView responds correctly to HEAD
     187        """
     188        response = self.client.head('/template/simple/bar/')
     189        self.assertEqual(response.status_code, 200)
     190        self.assertEqual(response.content, '')
     191
    170192    def test_get_template_attribute(self):
    171193        """
    172194        Test a view that renders a template on GET with the template name as
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index c06b5d8..46dbc82 100644
    a b from django.views.generic import TemplateView  
    33from django.views.decorators.cache import cache_page
    44
    55import views
     6import base
    67
    78
    89urlpatterns = patterns('',
    urlpatterns = patterns('',  
    1011    #(r'^about/login-required/$',
    1112    #    views.DecoratedAboutView()),
    1213
     14    # Base
     15    (r'^simple/$',
     16        base.SimpleView.as_view()),
     17
    1318    # TemplateView
    1419    (r'^template/no_template/$',
    1520        TemplateView.as_view()),
Back to Top