0

Hi I have a model which stores users actions. This model is used for showing notifications to logged in users. Now the issue is code works fine with logged in users, however if I open homepage of application after logout, following error appears.

TypeError at /

'AnonymousUser' object is not iterable

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.10
Exception Type:     TypeError
Exception Value:    

**'AnonymousUser' object is not iterable**

Exception Location:     /Library/Python/2.7/site-packages/django/utils/functional.py in inner, line 235
Python Executable:  /usr/bin/python
Python Version:     2.7.10

I think the issue is because of template context processor I am using in my application.

Please help on this. context code

from models import notifications


def activ_notification(request):
    active = notifications.objects.filter(to_user=request.user,viewed=False)[:10]

    return({'alert':active})

Settings

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'task.notification.activ_notification',
            ],
        },
    },
]
2ps
  • 15,099
  • 2
  • 27
  • 47
Pulkit Sharma
  • 264
  • 1
  • 2
  • 18

1 Answers1

2

Try just add is_authenticated check inside your view:

def activ_notification(request):
    active = []
    if request.user.is_authenticated():    
        active = notifications.objects.filter(to_user=request.user,viewed=False)[:10]

    return({'alert':active})
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100