Django 1.9:' AnonymousUser'对象不可迭代

时间:2016-07-19 00:01:50

标签: django

我正在开发一个Django 1.9项目,当我尝试以注销用户的身份查看我网站的主页时突然出现此错误。

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.9.7
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'captcha',
 'debug_toolbar',
 'django_extensions',
 'djstripe',
 'crm',
 'launch',
 'rentals',
 'widget_tweaks']
Installed Middleware:
[u'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  172.                     response = response.render()

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/template/response.py" in render
  160.             self.content = self.rendered_content

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  137.         content = template.render(context, self._request)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/template/backends/django.py" in render
  95.             return self.template.render(context)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/template/base.py" in render
  204.                 with context.bind_template(self):

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" in __enter__
  17.             return self.gen.next()

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in _request_context_bind_template
  79.             context = processor(self.request)

File "/Users/me/projects/rentalguru/src/rentals/processors.py" in allCategories
  4.     expense_categories = ExpenseCategory.objects.filter(user=request.user)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/query.py" in filter
  790.         return self._filter_or_exclude(False, *args, **kwargs)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  808.             clone.query.add_q(Q(*args, **kwargs))

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1243.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1269.                     allow_joins=allow_joins, split_subq=split_subq,

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1174.             self.check_related_objects(field, value, opts)

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/db/models/sql/query.py" in check_related_objects
  1073.                 for v in value:

File "/Users/me/projects/rentalguru/lib/python2.7/site-packages/django/utils/functional.py" in inner
  205.         return func(self._wrapped, *args)

Exception Type: TypeError at /
Exception Value: 'AnonymousUser' object is not iterable

我不确定导致此问题的原因,并且此错误只有一个Google搜索结果(针对此网站)。我在代码中搜索了is_authenticated()方法并且找不到任何内容,所以我不确定在哪里转。通过堆栈跟踪工作并没有显示任何有用的(对我来说)信息。

以下是我的观点:

class HomePageView(TemplateView):
    template_name = 'home.html'

我无法告诉登录修饰符或包装函数。

如果我需要发布任何其他内容,请告诉我。

编辑:在询问模型时,以下是违规的上下文处理器:

def allCategories(request):
    expense_categories = ExpenseCategory.objects.filter(user=request.user)
    tags = {'categories': expense_categories}
    return tags

1 个答案:

答案 0 :(得分:0)

我只是偶然发现了这个错误。谢天谢地,答案在评论中提供:

  

正如您所说,您尝试查看已注销用户的主页,并且此匿名用户的ExpenseCategory.objects.filter(user=request.user)过滤(因为您现在已注销)似乎是罪魁祸首。您应该只允许经过身份验证的用户访问此视图,或处理用户为匿名

的情况

至于我自己,我有一个视图,我在基于当前用户的模型上进行过滤,当用户是AnonymousUser时,该模型显然会失败。

我通过添加支票来解决这个问题:

if request.user.is_active:
    expense_categories = ExpenseCategory.objects.filter(user=request.user)

请注意,有些人似乎更愿意检查request.user.is_authenticated

如果您正在处理仅由登录用户访问的视图,您还可以使用@login_required装饰器。

供参考: