AnonymousUser.is_authenticated woes

34 views
Skip to first unread message

Stefan Bethke

unread,
Oct 11, 2018, 6:26:27 PM10/11/18
to django...@googlegroups.com
First time poster here,

I'm currently working on some unit tests of my Django app, and I'm running into a issue with is_authenticated.

I'm using Django 1.11.16 and Django CMS 3.5.2.

While trying to work with Django CMS menus, I get:

File "/usr/local/lib/python3.6/site-packages/menus/menu_pool.py", line 243, in get_renderer
return MenuRenderer(pool=self, request=request)
File "/usr/local/lib/python3.6/site-packages/menus/menu_pool.py", line 107, in __init__
self.draft_mode_active = use_draft(request)
File "/usr/local/lib/python3.6/site-packages/cms/utils/moderator.py", line 5, in use_draft
is_staff = (request.user.is_authenticated() and request.user.is_staff)
TypeError: 'property' object is not callable

Looking at the documentation, this should be fine. https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#attributes

Trying this in a shell:
./run.sh manage shell
Python 3.6.6 (default, Sep 12 2018, 02:15:29)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import AnonymousUser
>>> AnonymousUser.is_authenticated
<property object at 0x7fb8c93878b8>
>>> bool(AnonymousUser.is_authenticated)
True
>>> AnonymousUser.is_authenticated()
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'property' object is not callable

None of that seems right. Have I somehow managed to get something else pulled in?

In the actual application, not-logged in users see the right stuff, so apparently there it's working.


Stefan

--
Stefan Bethke <s...@lassitu.de> Fon +49 151 14070811

Markus Holtermann

unread,
Oct 11, 2018, 6:55:39 PM10/11/18
to django...@googlegroups.com
Hi Stefan,

You need to remove the parentheses `()` after `is_authenticated`.

Markus
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/E0552D52-C644-43AD-8F57-0DEAEC757445%40lassitu.de.
> For more options, visit https://groups.google.com/d/optout.

Simon Charette

unread,
Oct 11, 2018, 6:58:33 PM10/11/18
to Django users
Are you sure you are using Django 1.11?

What does ./run.sh manage --version yields?

User/AnonymousUser was made a property in Django 2.0+ which I assume your version of Django CMS is not compatible with.

Best,
Simon

Simon Charette

unread,
Oct 11, 2018, 7:01:53 PM10/11/18
to Django users
Django 1.11 correctly returns a callable on AnonymouUser.is_authenticated accesses.


This was changed in Django 2.0 after a deprecation period of a few releases


Which is why I suspect you have Django 2.0+ installed.

Cheers,
Simon

Stefan Bethke

unread,
Oct 11, 2018, 9:19:20 PM10/11/18
to django...@googlegroups.com
Am 11.10.2018 um 18:44 schrieb Stefan Bethke <s...@lassitu.de>:
from django.contrib.auth.models import AnonymousUser
AnonymousUser.is_authenticated
<property object at 0x7fb8c93878b8>

The answer is of course to use an instance, not the class:

from django.contrib.auth.models import AnonymousUser
u = AnonymousUser()
u.is_authenticated
CallableBool(False)
u.is_authenticated()
False
bool(u.is_authenticated)
False

Doh!

Sorry for wasting all of yours time.
Reply all
Reply to author
Forward
0 new messages