Thursday, September 28, 2017

Re: Problem testing user is None when extending auth backend.



On Thursday, September 28, 2017 at 2:16:44 PM UTC+1, Derek wrote:
Note that
user.is_authenticated()
has become
user.is_authenticated
in Django 1.10
 
Still on 1.8 in this case

Otherwise if user is really (or could really) be None, then try... except may be better:
 
Thanks, will try it. I still do not understand why checking for not None does not work as I expected.

Importing the code into the Django shell and calling authenticate() with an invalid username/password combination works as expected

    if user.is_authenticated() and user.is_staff:
do something
else:
do something else
except AttributeError as err:
 log.error('User not found:%s' % err)



On Thursday, 28 September 2017 13:50:27 UTC+2, graeme wrote:
In a custom authentication backend, I was getting this error with this (previous developer's!) code:
   if user.is_authenticated() and user.is_staff:  AttributeError: 'NoneType' object has no attribute 'is_authenticated'

The cause seemed clear, the parent classes method was run first with super and returned None when it failed to authenticate, so I tried to fix by testing whether user is None, but I still get this:

if (user is not None) and user.is_authenticated() and user.is_staff:  AttributeError: 'NoneType' object has no attribute 'is_authenticated'

I am probably missing something obvious, but if user is None, only (user is not None) will be evaluated there should be no error, if user is not None, I should not have a NoneType in the error.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/78d2b6a8-9d80-4cd7-96e1-00ff7a9f2260%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment