1

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/luisa@gmail.com/ URL when the user who performs the request is loggedIn, the request is O.K

[08/Sep/2017 23:56:20] "GET /accounts/profiles/luisa@gmail.com/ 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/luisa@gmail.com/ url I get this message:

TypeError: 'AnonymousUser' object is not iterable
[09/Sep/2017 00:00:50] "GET /accounts/profiles/luisa@gmail.com/ 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?

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
bgarcial
  • 2,915
  • 10
  • 56
  • 123
  • 1
    While trying to diagnose the problem you create another problem, that has nothing to do with your original problem. Keep debugging statements simple. If you want to understand why `reverse_lazy("accounts:detail", email=email)` with a queryset will never ever work, ask a separate question (hint: a queryset is not an email address). –  Sep 09 '17 at 02:34
  • 1
    The problem is with your two custom manager methods `is_following` and `recommended`. Remove your debugging and replace with the code for those manager methods. –  Sep 09 '17 at 02:40
  • @Melvyni t's true about of a queryset is not email address, you have reason. In relation to my error, it's true too, puntually, is with the custom manager method `is_following()` I don't understand much when you tell me **Remove your debugging and replace with the code for those manager methods** my apologies ... – bgarcial Sep 09 '17 at 14:56
  • 1
    I meant, show the code for `is_following()`, then we can better help you. To keep the question clean, best to remove your debugging code. Right now, we can't tell why `is_following()` tries to iterate an `AnonymousUser` object. –  Sep 11 '17 at 15:51
  • @Melvyn I've add the custom managers methods `is_following()` and `recommended()` Thanks for their orientation. – bgarcial Sep 11 '17 at 16:17

1 Answers1

1

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):
halfer
  • 19,824
  • 17
  • 99
  • 186
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • But the `LoginRequired` class is to redirects to login if user is anonymous, I want allow to the anonymous users access and view the `UserDetailView` – bgarcial Sep 09 '17 at 01:29
  • But this will act only when, unauthenticated user try to access user profile. – Astik Anand Sep 09 '17 at 02:40
  • And logically if one is not authenticated they should be redirected to login if they click on `detail profile`. – Astik Anand Sep 09 '17 at 02:42
  • And if someone is not logged in how they can accesss their profile detail?? – Astik Anand Sep 09 '17 at 02:43
  • I want that a anoymous user have the able of view the user detail profile of the users which if exists in the system. Something similar to when you can access to twitter or facebook profile of somebody without needed of create an account in facebook or twitter. – bgarcial Sep 09 '17 at 14:59
  • Hi Astik. When chatty material is removed from your posts, please do not add it back. Mutual editing is a key part of how Stack Overflow works. – halfer Jul 16 '18 at 17:51