'AnonymousUser' object is not iterable - accessing to views by anonymous and authenticateds users

1.1k Views Asked by At

I have the following Urls:

The main urls.py:

url(r'^accounts/profiles/', include('accounts.urls', namespace = 'accounts')),

The accounts/urls.py

url(r"^(?P<email>[\w.@+-]+)/$", views.UserDetailView.as_view(), name='detail'),

I've created the UserDetailView to view the data of an user

User = get_user_model()

class UserDetailView(UserProfileDataMixin, generic.DetailView):

    template_name = 'accounts/user_detail.html'
    queryset = User.objects.all()

    def get_object(self):
        return get_object_or_404(
                    User,
                    email__iexact=self.kwargs.get("email")
                    )

    def get_context_data(self, *args, **kwargs):
        context = super(UserDetailView, self).get_context_data(*args, **kwargs)
        user = self.request.user
        following = UserProfile.objects.is_following(self.request.user, self.get_object())
        context['following'] = following
        context['recommended'] = UserProfile.objects.recommended(self.request.user)
        return context

When I access to /accounts/profiles/[email protected]/ URL when the user who performs the request is loggedIn, the request is O.K

[08/Sep/2017 23:56:20] "GET /accounts/profiles/[email protected]/ HTTP/1.1" 200 23577

But, I want that this view to be acceded by anonymous users or unauthenticated users, which does not register in my application. When an anonymous user access to /accounts/profiles/[email protected]/ url I get this message:

TypeError: 'AnonymousUser' object is not iterable
[09/Sep/2017 00:00:50] "GET /accounts/profiles/[email protected]/ HTTP/1.1" 500 151513

My custom manager method is_following() is:

def is_following(self, user, followed_by_user):
    user_profile, created = UserProfile.objects.get_or_create(user=user)
    if created:
        return False
    if followed_by_user in user_profile.following.all():
        return True
    return False

and the recommended() method:

def recommended(self, user, limit_to=10):
    print(user)
    profile = user.profile
    # my profile

    following = profile.following.all()
    # profile of the people that I follow

    following = profile.get_following()
    # TO avoid recommend myself

    # Exclude the users recommendations which I already follow 
    qs = self.get_queryset().exclude(user__in=following).exclude(id=profile.id).order_by("?")[:limit_to]
    return qs

How to can I allow my UserDetailView to be accessed by anonymous users?

1

There are 1 best solutions below

6
On

First create a LoginRequired class.

from django.contrib.auth.decorators import login_required
from django.views.generic import View
from django.utils.decorators import method_decorator


class LoginRequired(View):
    """
    Redirects to login if user is anonymous
    """
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequired, self).dispatch(*args, **kwargs)

And then:

class UserDetailView(LoginRequired,UserProfileDataMixin, generic.DetailView):