From 12cf9d2be3cccb2ff63d78e93f97188040488a3d Mon Sep 17 00:00:00 2001 From: George Hickman Date: Thu, 22 Nov 2012 20:10:01 +0100 Subject: [PATCH] [1.5.x] Fixed #19316 -- Set View args/kwargs/request before dispatch Backport of ea6b95db from master. --- django/views/generic/base.py | 6 +++--- tests/regressiontests/generic_views/base.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/django/views/generic/base.py b/django/views/generic/base.py index d0045a2ef0..9c82a29d8a 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -62,6 +62,9 @@ class View(object): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get + self.request = request + self.args = args + self.kwargs = kwargs return self.dispatch(request, *args, **kwargs) # take name and docstring from class @@ -80,9 +83,6 @@ class View(object): handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed - self.request = request - self.args = args - self.kwargs = kwargs return handler(request, *args, **kwargs) def http_method_not_allowed(self, request, *args, **kwargs): diff --git a/tests/regressiontests/generic_views/base.py b/tests/regressiontests/generic_views/base.py index 439b0d7327..c7ad7a0deb 100644 --- a/tests/regressiontests/generic_views/base.py +++ b/tests/regressiontests/generic_views/base.py @@ -216,6 +216,17 @@ class ViewTest(unittest.TestCase): response_allows = set(response['Allow'].split(', ')) self.assertEqual(set(expected_methods + ('OPTIONS',)), response_allows) + def test_args_kwargs_request_on_self(self): + """ + Test a view only has args, kwargs & request once `as_view` + has been called. + """ + bare_view = InstanceView() + view = InstanceView.as_view()(self.rf.get('/')) + for attribute in ('args', 'kwargs', 'request'): + self.assertNotIn(attribute, dir(bare_view)) + self.assertIn(attribute, dir(view)) + class TemplateViewTest(TestCase): urls = 'regressiontests.generic_views.urls'