2016-12-09 17 views
0

こんにちは私はユーザーのアクションを格納するモデルを持っています。このモデルは、ログインしたユーザーへの通知を表示するために使用されます。 ログオンしたユーザーのコードが問題なく動作しますが、ログアウト後にアプリケーションのホームページを開くと、次のエラーが表示されます。Django:匿名ユーザーのコンテキストプロセッサーエラー

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 

問題は私のアプリケーションで使用しているテンプレートコンテキストプロセッサのためだと思います。

助けてください。 コンテキストコード

from models import notifications 


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

    return({'alert':active}) 

設定

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', 
      ], 
     }, 
    }, 
] 

答えて

1

ちょうどあなたのビュー内is_authenticatedチェックを追加してみてください。

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

    return({'alert':active}) 
+0

これが正解です。ありがとうございました –

関連する問題