{{{
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.
* 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>