Thursday, September 28, 2017

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

Note that
user.is_authenticated()
has become
user.is_authenticated
in Django 1.10

Otherwise if user is really (or could really) be None, then try... except may be better:

try:
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/9899cf8f-a1b2-49e6-8a17-78260a60f0af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment