diff --git a/django/core/context_processors.py b/django/core/context_processors.py index 3c826b1a7d..55c19c17e8 100644 --- a/django/core/context_processors.py +++ b/django/core/context_processors.py @@ -13,11 +13,19 @@ def auth(request): """ Returns context variables required by apps that use Django's authentication system. + + If there is no 'user' attribute in the request, uses AnonymousUser (from + django.contrib.auth). """ + if hasattr(request, 'user'): + user = request.user + else: + from django.contrib.auth.models import AnonymousUser + user = AnonymousUser() return { - 'user': request.user, - 'messages': request.user.get_and_delete_messages(), - 'perms': PermWrapper(request.user), + 'user': user, + 'messages': user.get_and_delete_messages(), + 'perms': PermWrapper(user), } def debug(request):