I have a django follow system in which a user can follow other users and also unfollow them such like in instagram. In order to follow a user, the person has to visit someone's profile and press the follow button, if the user already follows that person then the user can press the unfollow button. Also in a profile there is a follower count whcih displays the number of followers a user has and a following count that displays the number of users that user is following(again like in instagram).
Well everything was almost done and I was just missing the follower count in which I added a signals.py file to do it. When that file was finished I realized that now the buttons are not working because when the user presses the follow button it doesnt follow the user. What can I do to make this buttons do their job which is follow and unfollow people? please any idea helps.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(upload_to='profile_pics', null=True, blank=True, default='default.png')
bio = models.CharField(max_length=400, default=1, null=True)
connection = models.CharField(max_length = 100, blank=True)
follower = models.IntegerField(default=0)
following = models.IntegerField(default=0)
def __str__(self):
return f'{self.user.username} Profile'
class Following(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
followed = models.ManyToManyField(User, related_name="followed")
follower = models.ManyToManyField(User, related_name="follower")
@classmethod
def follow(cls, user, another_account):
obj, create = cls.objects.get_or_create(user = user)
obj.followed.add(another_account)
print("followed")
@classmethod
def unfollow(cls, user, another_account):
obj, create = cls.objects.get_or_create(user = user)
obj.followed.remove(another_account)
print("unfollowed")
def __str__(self):
return f'{self.user.username} Profile'
views.py
def profile(request, username=None):
profile, created = Profile.objects.get_or_create(user=request.user)
user = User.objects.filter(username=username)
if username:
post_owner = get_object_or_404(User, username=username)
profile_bio = Profile.objects.filter(user_id=post_owner)
user_posts = Post.objects.filter(user_id=post_owner)
user = user[0]
conn = profile.connection
is_following = Following.objects.filter(user=request.user, followed=user)
following_obj = Following.objects.get(user=user)
follower = following_obj.follower.count()
following = following_obj.followed.count()
else:
post_owner = request.user
user_posts = Post.objects.filter(user=request.user)
profile_bio = Profile.objetcs.filter(user=request.user)
args1 = {
'user_obj':user,
'conn':conn,
'post_owner': post_owner,
'user_posts': user_posts,
'follower': follower,
'following': following,
'connection': is_following,
'profile_bio': profile_bio,
}
return render(request, 'profile.html', args1)
def follow(request, username):
main_user = request.user
to_follow = User.objects.get(username=username)
following = Following.objects.filter(user = main_user, followed = to_follow)
is_following = True if following else False
if is_following:
Following.unfollow(main_user, to_follow)
is_following = False
else:
Following.follow(main_user, to_follow)
is_following = True
resp = {
'following': is_following,
}
response = json.dumps(resp)
return HttpResponse(response, content_type="application/json")
signals.py
@receiver(m2m_changed, sender = Following.followed.through) # which list is changed
def add_follower(sender, instance, action, reverse, pk_set, **kwargs):
followed_users = [] # list of users main (logged ) user have followed
logged_user = User.objects.get(username = instance) # user who followed other users
for i in pk_set:
user = User.objects.get(pk = i)
following_obj = Following.objects.get(user = user)
followed_users.append(following_obj)
if action == "pre_add":
for i in followed_users:
i.follower.add(logged_user)
i.save()
if action == "pre_remove":
for i in followed_users:
i.follower.remove(logged_user)
i.save()
profile.html
<div class="header-item">
followers: {{ follower }}
</div>
<div class="header-item">
following: {{ following }}
</div>
{% if connection %}
<a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Unfollow</a>
{% elif not connection %}
<a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Follow</a>
{% endif %}
Any questions please let me know in the comments;)