'AnonymousUser'对象不可迭代? Django的

时间:2017-04-21 13:43:53

标签: django

我正在创建用户创建的所有帖子的摘要页面,并将这些帖子返回到摘要页面。我得到上面的错误?

这是在抱怨这条线:

uploaded_aircraft = Aircraft.objects.filter(user=request.user)

查看

def aircraft_create(request):
  form = aircraft_form(request.POST or None)
  if form.is_valid():
    instance = form.save(commit=False)
    instance.user = request.user
    instance.save()
    messages.success(request, "Your upload has been successfully added!")
    return HttpResponseRedirect(instance.get_absolute_url())
  else:
    messages.error(request, "There seems to be something wrong. Have a look again..!")
  context = {"form":form,}
  return render(request,'aircraft/aircraft_form.html', context)

模型

class Aircraft(AircraftModelBase):
    user = models.ForeignKey(User)
    manufacturer = SortableForeignKey(Manufacturer)
    aircraft_type = SortableForeignKey(AircraftType)

查看

def account_overview(request):
    fav_aircraft = FavoritedAircraft.objects.filter(user__id=request.user.id)
    fav_airline = FavoritedAirline.objects.filter(user__id=request.user.id)

    uploaded_aircraft = Aircraft.objects.filter(user=request.user)

    return render(request,'account/account_overview.html', {'favAircraft':fav_aircraft,
                                                             'favAirline':fav_airline,
                                                             'UploadedAircraft':uploaded_aircraft})

回溯

Traceback (most recent call last):
  File "/home/csunix/scpython/lib/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/csunix/scpython/lib/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/cserv1_a/soc_ug/sc13nh/env/v10/accounts/views.py", line 57, in account_overview
    uploaded_aircraft = Aircraft.objects.filter(user=request.user)
  File "/home/csunix/scpython/lib/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/csunix/scpython/lib/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/csunix/scpython/lib/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/csunix/scpython/lib/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/csunix/scpython/lib/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/home/csunix/scpython/lib/django/db/models/sql/query.py", line 1174, in build_filter
    self.check_related_objects(field, value, opts)
  File "/home/csunix/scpython/lib/django/db/models/sql/query.py", line 1073, in check_related_objects
    for v in value:
  File "/home/csunix/scpython/lib/django/utils/functional.py", line 205, in inner
    return func(self._wrapped, *args)
TypeError: 'AnonymousUser' object is not iterable

这里似乎有什么实际问题?

5 个答案:

答案 0 :(得分:4)

错误告诉您request.user是匿名用户。这意味着他们没有登录,因此尝试使用filter(user=request.user)进行过滤是没有意义的。

最简单的解决方法是使用login_required装饰器,以确保只有登录用户才能访问该视图。

from django.contrib.auth.decorators import login_required

@login_required
def account_overview(request):
    ...

答案 1 :(得分:1)

我通过“@login_required(login_url="user:login")”找出了这个问题。

@login_required(login_url="user:login")
def dashboard(request):
    articles = Article.objects.filter(author = request.user)

    return render(request,"dashboard.html",{"articles": articles})

答案 2 :(得分:0)

def account_overview(request):

 if request.user.is_authenticated:  # Notice this condition
    fav_aircraft = FavoritedAircraft.objects.filter(user__id=request.user.id)
    fav_airline = FavoritedAirline.objects.filter(user__id=request.user.id)

    uploaded_aircraft = Aircraft.objects.filter(user=request.user)
    return render(request,'account/account_overview.html', {'favAircraft':fav_aircraft,
                                                         'favAirline':fav_airline,
                                                         'UploadedAircraft':uploaded_aircraft})
   return render(request,'account/account_overview.html')

这应该可以解决您的问题 使用is_authenticated条件语句包装视图以仅在用户通过身份验证时才返回上下文。

答案 3 :(得分:0)

我刚刚遇到了同样的问题,并通过注销然后重新签名来解决。

就目前而言,我认为这可能很容易解决。

答案 4 :(得分:0)

尽管在您的情况下它不起作用(因为您使用方法而不是类),但您可以使用 LoginRequiredMixin 解决此问题:

from django.contrib.auth.mixins import LoginRequiredMixin

class MyView (LoginRequiredMixin, View):
     login_url = '/ login /'
     redirect_field_name = 'redirect_to'