Fixed #19316 -- Set View args/kwargs/request before dispatch

This commit is contained in:
George Hickman 2012-11-22 20:10:01 +01:00 committed by Claude Paroz
parent 71a3162a70
commit ea6b95dbec
2 changed files with 14 additions and 3 deletions

View File

@ -62,6 +62,9 @@ class View(object):
self = cls(**initkwargs) self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'): if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs) return self.dispatch(request, *args, **kwargs)
# take name and docstring from class # take name and docstring from class
@ -80,9 +83,6 @@ class View(object):
handler = getattr(self, request.method.lower(), self.http_method_not_allowed) handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else: else:
handler = self.http_method_not_allowed handler = self.http_method_not_allowed
self.request = request
self.args = args
self.kwargs = kwargs
return handler(request, *args, **kwargs) return handler(request, *args, **kwargs)
def http_method_not_allowed(self, request, *args, **kwargs): def http_method_not_allowed(self, request, *args, **kwargs):

View File

@ -216,6 +216,17 @@ class ViewTest(unittest.TestCase):
response_allows = set(response['Allow'].split(', ')) response_allows = set(response['Allow'].split(', '))
self.assertEqual(set(expected_methods + ('OPTIONS',)), response_allows) 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): class TemplateViewTest(TestCase):
urls = 'regressiontests.generic_views.urls' urls = 'regressiontests.generic_views.urls'