saurabh singh
saurabh singh

Reputation: 67

Follow & unfollow system django

i have follow and unfollow system for my app but when i want that using the if statement html changes from follow to unfollow automatically.. but unfortunately my for loop always gives False in views.py and i don't know what is wrong. here is my models.py file

class FollowUser(models.Model):
    profile = models.ForeignKey(to=Profile, on_delete=models.CASCADE)
    Followed_by = models.ForeignKey(to=User, on_delete=models.CASCADE)

    def __str__(self):
        return "%s" % self.Followed_by

here is my views.py file

class UserListView(ListView):
    model = Profile
    context_object_name = 'users'
    template_name = 'dashboard/user_list.html'

    def get_queryset(self):
        si = self.request.GET.get("si")
        if si == None:
            si = ""
        profList = Profile.objects.filter(Q(phone__icontains = si) | Q(location__icontains = si) | Q(gender__icontains = si) | Q(organization_name__icontains = si)).order_by("-id");
        for p1 in profList:
            p1.followed = False
            ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user.profile.id)
            if ob:
                p1.followed = True
        return profList

Upvotes: 1

Views: 122

Answers (1)

saurabhjdsingh
saurabhjdsingh

Reputation: 48

Instead of using the user.profile.id to search the user by profile id , you should search by its user only.

ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user.profile.id)

try:

ob = FollowUser.objects.filter(profile=p1, Followed_by=self.request.user)

Upvotes: 1

Related Questions