[Django] #25798: RedirectView providing different request object to decorator

13 views
Skip to first unread message

Django

unread,
Nov 22, 2015, 2:48:20 PM11/22/15
to django-...@googlegroups.com
#25798: RedirectView providing different request object to decorator
-------------------------------+-----------------------
Reporter: connexion2000 | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.9rc1
Severity: Normal | Keywords: decorator
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------
I have my own simple decorator:

{{{
def ensure_user_has_profile(view_func):
def _decorator(request, *args, **kwargs):
if request.user:
if request.user.is_authenticated():
if not hasattr(request.user, 'profile'):
profile = Profile()
profile.user = request.user
profile.save()

response = view_func(request, *args, **kwargs)
return response

return wraps(view_func)(_decorator)
}}}


However when RedirectView is decorated with above decorator it causes an
error:

{{{
'HomeView' object has no attribute 'user'
}}}
to make it work I need to access user by typing: request.request.user.
Then it works with RedirectView, but not with other views.

--
Ticket URL: <https://code.djangoproject.com/ticket/25798>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 22, 2015, 3:41:52 PM11/22/15
to django-...@googlegroups.com
#25798: RedirectView providing different request object to decorator
-------------------------------+--------------------------------------
Reporter: connexion2000 | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.9rc1
Severity: Normal | Resolution: invalid

Keywords: decorator | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by knbk):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

What you pass as `request` is actually the view instance. You cannot
decorate a class method directly if your `_decorator()` function does not
accept `self` as its first argument. Instead, you need to use
`method_decorator` or decorate the view function returned by
`View.as_view()`.

This is also explained in the documentation on
[https://docs.djangoproject.com/en/1.8/topics/class-based-views/intro/#id2
decorating class-based views].

--
Ticket URL: <https://code.djangoproject.com/ticket/25798#comment:1>

Reply all
Reply to author
Forward
0 new messages