3

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).

3 Answers 3

2

Works properly in 1.3:

class TestView(DetailView):
    def get(self, request, **kwargs):
        import ipdb; ipdb.set_trace()

ipdb> request.user
<User: zk>
ipdb> request.user.is_authenticated()
True

Possibly a bug?

Sign up to request clarification or add additional context in comments.

11 Comments

That must be someting else. I;ve created testproject with testapp with testmodel with testview using django 1.3 and it's still same result. Maybe there is someting I'm missing. Could you please post you full test code?
That is the full code. The only thing missing is the urlconf, which is as you'd expect: url(r'^test', TestView.as_view())
It appears that this bug is present only using firefox which is quite odd. :/
Checked in another 1.3 project as well, not sure what you could be doing wrong.
I also use Firefox, and don't see this behavior.
|
1

Try using the decorators from django.contrib.auth.decorators. In your urls.py, you can do something like:

from django.contrib.auth.decorators import login_required

...
url(r'^something/?$', login_required(MyDetailView.as_view()))
...

For checking permissions, you can use the premissions_required decorator. For more info, check out the docs: https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator

3 Comments

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 from django.contrib.flatpages).
Ah. In that case, I'd recommend writing your own views, since they seem to require more specific behavior. No sense in trying to shoehorn this behavior into generic views.
There's nothing wrong about generic views. The problem appears in class-based views, in function-based generic views it works perfectly. Since django has introduced class-based [generic] views it seems proper to use them.
0

I use mixins for class based views. In this case, you can do it like this:

Django Class-Based Generic Views and Authentication

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.