I'm trying to check if user.is_authenticated()
or if user.has_perm()
but it seems impossible extending django Class-based genering views. The only method I found where the request
appears is get()
.
class MyDetailView(DetailView):
def get(self, request, **kwargs):
import pdb
pdb.set_trace()
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
there I found that request.user
is instance of AnonymusClass
no matter if I'm logged in or not.
(Pdb) request.user.__class__
<class 'django.contrib.auth.models.AnonymousUser'>
so checking for authentification or perms will always fail:
(Pdb) self.request.user.is_authenticated()
False
I've tried overriding other methods such as get_object()
, get_context_data()
adn others. I each of them there is self.request
attribute available, but user
is still Anonymus
.
So my question is: How on Earth am I supposed to check if user is logged in using Class-based views!?
Does it mean I have to (go back and) use function-based views?
I'm using Python 2.7.1+
and Django version 1.4 pre-alpha SVN-16627
In response to EVIAAC post:
Using login_required
or permissions_required
decorators is not an option. I need to check for permissions/logon after I retrieve object: if object has boolean field registration_required
set to True
only reqistered users will be able to see the page, others will get redirected to logon page (example behavior borrowed from django.contrib.flatpages
).