From c694587ebbf7a45d3e6f91f5bfc4e81001c03d97 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 16 Sep 2007 11:27:40 +0000 Subject: [PATCH] Fixed #4049 -- Improved error handling in auth() context processor. Based on a patch from gregorth. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6356 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/context_processors.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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):