summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2013-12-16 16:45:18 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2013-12-16 16:58:08 +0100
commite2f142030b81a37e1c3187f5d336dcb6014fd1c0 (patch)
tree59d3b421f28d38b31cd3b68841bc01a92683ae1e
parent3eb58f0dd11ae10fa5741afbc43e229332a4373a (diff)
Fixed #21564 -- Use local request object when possible in generic views.
Thanks to trac user adepue for the report and original patch.
-rw-r--r--django/views/generic/base.py6
-rw-r--r--tests/generic_views/test_base.py18
2 files changed, 21 insertions, 3 deletions
diff --git a/django/views/generic/base.py b/django/views/generic/base.py
index 745c5ec1fc..d66f45b599 100644
--- a/django/views/generic/base.py
+++ b/django/views/generic/base.py
@@ -90,7 +90,7 @@ class View(object):
logger.warning('Method Not Allowed (%s): %s', request.method, request.path,
extra={
'status_code': 405,
- 'request': self.request
+ 'request': request
}
)
return http.HttpResponseNotAllowed(self._allowed_methods())
@@ -193,10 +193,10 @@ class RedirectView(View):
else:
return http.HttpResponseRedirect(url)
else:
- logger.warning('Gone: %s', self.request.path,
+ logger.warning('Gone: %s', request.path,
extra={
'status_code': 410,
- 'request': self.request
+ 'request': request
})
return http.HttpResponseGone()
diff --git a/tests/generic_views/test_base.py b/tests/generic_views/test_base.py
index 66ce224162..311ecf29af 100644
--- a/tests/generic_views/test_base.py
+++ b/tests/generic_views/test_base.py
@@ -228,6 +228,15 @@ class ViewTest(unittest.TestCase):
self.assertNotIn(attribute, dir(bare_view))
self.assertIn(attribute, dir(view))
+ def test_direct_instantiation(self):
+ """
+ It should be possible to use the view by directly instantiating it
+ without going through .as_view() (#21564).
+ """
+ view = PostOnlyView()
+ response = view.dispatch(self.rf.head('/'))
+ self.assertEqual(response.status_code, 405)
+
class TemplateViewTest(TestCase):
urls = 'generic_views.urls'
@@ -421,6 +430,15 @@ class RedirectViewTest(TestCase):
response = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))
self.assertEqual(response.status_code, 301)
+ def test_direct_instantiation(self):
+ """
+ It should be possible to use the view without going through .as_view()
+ (#21564).
+ """
+ view = RedirectView()
+ response = view.dispatch(self.rf.head('/foo/'))
+ self.assertEqual(response.status_code, 410)
+
class GetContextDataTest(unittest.TestCase):