开发者

Where in Django does a user become an AnonymousUser?

I'm trying find out where/when exactly request.user becomes an AnonymousUser. I've been searching through the entire Auth backend but I can't seem find it. Am I looking in the wrong place?

I'm aware that every user that isn't an开发者_StackOverflow社区 Authenticated user becomes an AnonymousUser, but I need to know where/when this happens for some code I'm building.

Any help would be appreciated.


contrib\auth\__init__.py:80

def get_user(request):
    from django.contrib.auth.models import AnonymousUser
    try:
        user_id = request.session[SESSION_KEY]
        backend_path = request.session[BACKEND_SESSION_KEY]
        backend = load_backend(backend_path)
        user = backend.get_user(user_id) or AnonymousUser()
    except KeyError:
        user = AnonymousUser()
    return user


it's all in django.contrib.auth.middleware module - look here: https://github.com/django/django/blob/master/django/contrib/auth/middleware.py#L49 for details.


logout method in "contrib/auth/init.py" also affect AnonymousUser() to request.user, look at the code below:

if hasattr(request, 'user'):
    from django.contrib.auth.models import AnonymousUser
    request.user = AnonymousUser()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜