【发布时间】:2016-06-07 15:11:31
【问题描述】:
我需要获取用户信息,但我只有request.user,这给了我一个django.contrib.auth.models.AnonymousUser。我知道 AnonymousUser 的 id、用户名……是空的。如何获取用户信息?
【问题讨论】:
-
用户没有任何信息,他们是匿名的。 (未认证)
标签: django django-authentication
我需要获取用户信息,但我只有request.user,这给了我一个django.contrib.auth.models.AnonymousUser。我知道 AnonymousUser 的 id、用户名……是空的。如何获取用户信息?
【问题讨论】:
标签: django django-authentication
你不能直接使用 AnonymousUser,这个类只需要成为 Django 请求中的默认用户。
如果您有用户 ID,请执行以下操作:
from django.core.exceptions import PermissionDenied
from django.contrib.auth.models import User
[... other code there ...]
try:
user = User.objects.get(pk=user_id)
except User.DoesNotExist:
raise PermissionDenied
[... other code there ...]
【讨论】: