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

2024/10/5 3:29:13

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_createlogin(request, user)File "/base/data/home/apps/s~XXXXX/1.356802202883392818/django/contrib/auth/__init__.py", line 82, in loginrequest.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?

Answer

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

https://en.xdnf.cn/q/70531.html

Related Q&A

python identity dictionary [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to make a python dictionary that returns key for keys missing from the dictionary instead of raising KeyError? I need…

Whats a good library to manipulate Apache2 config files? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

AttributeError: module object has no attribute webdriver

AttributeError: module object has no attribute webdriverwhy this error happen when write import selenium and when write code like this no error happenfrom selenium import webdriver

Mask Ocean or Land from data using Cartopy

I would like to mask the Land area from Sea Surface Temperature Data over the globe. I am using Cartopy to plot the data.import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs fr…

How to map func_closure entries to variable names?

I have a lambda object that is created in this function:def add_url_rule(self, rule, endpoint=None, view_func=None, **options):self.record(lambda s:s.add_url_rule(rule, endpoint, view_func, **options))…

How to use the convertScaleAbs() function in OpenCV?

I am trying to convert an image back to grayscale after applying Sobel filtering on it. I have the following code: import numpy as np import matplotlib.pyplot as plt import cv2image = cv2.imread("…

Register a Hello World DBus service, object and method using Python

Im trying to export a DBus service named com.example.HelloWorld, with an object /com/example/HelloWorld, and method com.example.HelloWorld.SayHello that prints "hello, world" if the method i…

Python 3 Timedelta OverflowError

I have a large database that I am loading into an in-memory cache. I have a process that does this iterating through the data day by day. Recently this process has started throwing the following error:…

Constructing hierarchy from dictionary/JSON

Im looking for a way to create hierarchy in form of child parent relationship between two or more instances of same class.How would one go about creating such objects from nested dictionary like in exa…

PyPy file append mode

I have code like this:f1 = open(file1, a) f2 = open(file1, a)f1.write(Test line 1\n) f2.write(Test line 2\n) f1.write(Test line 3\n) f2.write(Test line 4\n)When this code is run with standard Python 2.…