Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Set args, kwargs & request on self before dispatch is called. #532

Closed
wants to merge 1 commit into from

3 participants

@ghickman

Move the setting of args, kwargs & request on self into the nested
view function in as_view so they are set before dispatch is
called. This allows anyone wishing to put pre-dispatch code in an
overridden dispatch and still use the self versions of args, kwargs
and request.

@ghickman ghickman Set args, kwargs & request on self before dispatch is called.
Move the setting of args, kwargs & request on self into the nested
`view` function in `as_view` so they are set before dispatch is
called. This allows anyone wishing to put pre-dispatch code in an
overridden `dispatch` and still use the self versions of args, kwargs
and request.
ca2e6d6
@apollo13
Owner

Hey, I guess a bit of this was discussed on the most recent thread on the ml, but there are again some messages missing from the groups interface. Either way, could you please create a ticket and link to the pullrequest and mailinglist accordingly. Thanks!

@ghickman

@apollo13 Apologies, I didn't think to link to the ticket here too!

@claudep
Collaborator

Committed

@claudep claudep closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 18, 2012
  1. @ghickman

    Set args, kwargs & request on self before dispatch is called.

    ghickman authored
    Move the setting of args, kwargs & request on self into the nested
    `view` function in `as_view` so they are set before dispatch is
    called. This allows anyone wishing to put pre-dispatch code in an
    overridden `dispatch` and still use the self versions of args, kwargs
    and request.
This page is out of date. Refresh to see the latest.
View
6 django/views/generic/base.py
@@ -61,6 +61,9 @@ def view(request, *args, **kwargs):
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
@@ -79,9 +82,6 @@ def dispatch(self, request, *args, **kwargs):
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):
View
11 tests/regressiontests/generic_views/base.py
@@ -216,6 +216,17 @@ def _assert_allows(self, response, *expected_methods):
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'
Something went wrong with that request. Please try again.