Django App Engine:属性错误:“AnonymousUser”对象没有属性“backend”;

发布于 2025-01-06 11:30:09 字数 1069 浏览 1 评论 0原文

我正在使用 djangoappengine。当我尝试创建新用户、验证该用户并登录时,出现以下错误 AttributeError: 'AnonymousUser' object has no attribute 'backend'

我的代码很简单,看起来像:

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)

我只在生产中遇到以下错误,并且只是偶尔出现:

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'

我不确定,但我有一种不好的感觉,这个异常是由于高复制数据存储及其最终一致性造成的。我认为 authenticate() 保存用户值,login() 执行查询,但用户值尚未传播到 HRDS 中。谁能证实这是真的吗?如果是这样,将如何修复?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奶气 2025-01-13 11:30:09

当您保存用户时,它不会自动激活。请检查 AnonymousUser 的链接其中说明了您的用户如何成为AnonymousUser

因此,您必须设置所有可能使您的用户成为AnonymousUser的项目。在进行身份验证之前,请检查user.is_anonymous()

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

好菇凉咱不稀罕他 2025-01-13 11:30:09

有同样的问题。但问题是用户名太长,被截断为 30 个字符。在这里找到答案:

Django 注册表“AnonymousUser”对象没有属性“后端”

Had the same problem. But the problem was that the username was to long and it was truncated to 30 characters. Found the answer here:

Django Register Form 'AnonymousUser' object has no attribute 'backend'

稀香 2025-01-13 11:30:09

当用户未经身份验证时会发生这种情况。确保您使用相同的字段来登录后端...例如我使用电子邮件登录(使用电子邮件和密码的后端),所以

django.contrib.auth.authenticate(username=username, password=password)

如果对我来说

django.contrib.auth.authenticate(username=email, password=password)

后端在这里: http://www.micahcarrick.com/django-email-authentication.html

That happens when user was not authenticated. Be sure that you are using same field for loging Backend... for example I am loging with email (Backend that uses email and password) so

django.contrib.auth.authenticate(username=username, password=password)

if for me

django.contrib.auth.authenticate(username=email, password=password)

The backend is here: http://www.micahcarrick.com/django-email-authentication.html

蓝颜夕 2025-01-13 11:30:09

如果您在数据库中使用复制,并且在写入主/默认数据库时碰巧从只读副本读取数据,那么如果存在复制延迟,也可能会导致这种情况。
你会写信给主人
而验证将尝试从只读副本中读取,导致找不到用户。

解决方案是将路由器设置为使用默认数据库进行用户查询。

If you are using replication in your database and happen to be reading from Read replica while writing to your master/default database, then if there is a replication lag that could also cause this.
You will write to the master
while authenticate will try to read from read replica resulting in not finding the User.

The solution is to set up your routers to use default db for User queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文