Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django App Engine: AttributeError: 'AnonymousUser' object has no attribute 'backend'

I am using djangoappengine. When I try create a new user, authenticate that user, and log them in, I get the following error AttributeError: 'AnonymousUser' object has no attribute 'backend'.

My code is simple and looks like:

user = User.objects.create_user(username, username, password)
user.set_password(password)
user.save()

user = django.contrib.auth.authenticate(username=username, password=password)
django.contrib.auth.login(request, user)

I only get the following error on production and only occasionally:

web req_create: 'AnonymousUser' object has no attribute 'backend'
Traceback (most recent call last):
  File "/base/data/home/apps/s~XXXXX/1.356802202883392818/XXXX/XXX.py", line 332, in req_create
    login(request, user)
  File "/base/data/home/apps/s~XXXXX/1.356802202883392818/django/contrib/auth/__init__.py", line 82, in login
    request.session[BACKEND_SESSION_KEY] = user.backend
AttributeError: 'AnonymousUser' object has no attribute 'backend'

I am not sure, but I have a bad feeling that this exception is due to the high replication data store and its eventual consistency. I think that authenticate() saves the user value and that login() does a query but the user value has not yet propagated into the HRDS. Can anyone confirm this to be true? If so, how would it be fixed?

like image 266
speedplane Avatar asked Feb 15 '12 07:02

speedplane


1 Answers

When you save the user it will be not activate automatically. Please check the link for AnonymousUser which says how your user become AnonymousUser.

So you have to set all items which may be make your user as AnonymousUser. Before authenticate please check user.is_anonymous().

like image 134
Nilesh Avatar answered Oct 26 '22 04:10

Nilesh