Ideally this would be callable after `__init__()` so the view instance can
have some processing done before the request is known.
This would also be useful for providing better ways to test the CBVs.
--
Ticket URL: <https://code.djangoproject.com/ticket/20941>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* version: => master
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:1>
Comment (by tomchristie):
Okay, so what am I missing here?
{{{
request = self.factory.get('/')
response = MyView.as_view()(request)
}}}
What use case or functionality is this feature request trying to address
that isn't covered by the above?
--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:2>
Comment (by mjtamlyn):
That's not an instance! `as_view()` gives an opaque function with no
access to the instance of the class, so we can't test parts of it
individually.
The change in the dispatch method I mentioned means that the following
code currently does not work as you might expect (in particular,
`self.args` is not set):
{{{
request = self.factory.get('/')
view = MyView(**initkwargs)
response = view.dispatch(request, *args, **kwargs)
}}}
I acknowledge that this is perhaps not that different from calling
`as_view()`, but having an API along the lines of
`MyView().setup_request(request, *args, **kwargs)` would allow more atomic
testing of class based views in a nicer fashion. Compare below:
{{{
# current
book = Book.objects.create(...)
view = BookDetail()
view.request = request
view.args = ()
view.kwargs = {'pk': book.pk}
self.assertEqual(view.get_object(), book)
# possible better version
book = Book.objects.create(...)
view = BookDetail().setup_request(request, pk=book.pk)
self.assertEqual(view.get_object(), book)
}}}
It's not a big difference, but in my opinion it's a nice one.
--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:3>
Comment (by collinanderson):
If it helps, you can now introspect the view class and args from the
as_view() function. #24055
--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:4>
* status: new => closed
* resolution: => duplicate
Comment:
#29750 introduced `View.setup()` which seems to be exactly what Marc
proposed in his comment (with the name `setup_request`).
--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:5>