Hello,
I am hoping to get help with my follower system. I have read through some of the posts on here but haven’t been able to fix it. This follow system has been created using a video on YouTube. I am not good enough to code this myself entirely yet and appreciate help in getting in working.
What I Want To Happen
When I log in I can view the profile of a member. I can hit the follow button that will update that users follower count by 1 and my following count by 1. Then when I unfollow it works in reverse.
WHAT HAPPENS
When I log into the site the UNFOLLOW button is already showing. There’s no follow button and when I click on the unfollow button I receive a 404 error
The current path, users/followers_count
, didn’t match any of these.
I am hoping once the issue of why the follow button is not showing has been resolved I might be able to work on getting the system working. Hopefully resolving this will ensure the whole thing works
My url links - this one is in the app urls.py
path('users/<str:username>/followers_count', views.followers_count, name='followers_count'),
This one is in the main project urls.py
path('users/<str:username>/detail', views.follow_detail, name='user_detail'),
model.py
class FollowersCount(models.Model):
follower = models.CharField(max_length=1000)
user = models.CharField(max_length=1000)
def __str__(self):
return self.user
views.py
from django.shortcuts import render, redirect
from .models import FollowersCount
def follow_detail(request, username):
current_user = username
logged_in_user = request.user.username
user_followers = len(FollowersCount.objects.filter(user=current_user))
user_following = len(FollowersCount.objects.filter(follower=current_user))
user_followers_list = FollowersCount.objects.filter(user=current_user).values_list('follower', flat=True)
if logged_in_user in user_followers_list:
follow_button_value = 'unfollow'
else:
follow_button_value = 'follow'
return render(request, 'account/user/detail.html', {
'current_user': current_user,
'user_followers': user_followers,
'user_following': user_following,
'follow_button_value': follow_button_value
})
def followers_count(request, username):
if request.method == 'POST':
value = request.POST['value']
user = username
follower = request.user.username
if value == 'follow':
FollowersCount.objects.create(follower=follower, user=user)
else:
try:
followers_cnt = FollowersCount.objects.get(follower=follower, user=user)
followers_cnt.delete()
except FollowersCount.DoesNotExist:
pass
return redirect(f'/account/users/{user}/detail')
html page
<!--Follow button / number of followers-->
<form action="/users/{{current_user}}/followers_count" method="POST">
{% csrf_token %}
<input type="hidden" name="user" value="{{current_user}}" readonly/>
<input type="hidden" name="follower" value="{{user.username}}" readonly/>
{% if follow_button_value == 'follow' %}
<input type="hidden" name="value" value="follow" readonly/>
<button type="submit">
Follow
</button>
{% else %}
<input type="hidden" name="value" value="unfollow" readonly/>
<button type="submit">
Unfollow
</button>
{% endif %}
</form>
<div class="profile-stats">
<ul>
<li><span class="profile-stat-count">{{user_followers}}</span> followers</li>
<li><span class="profile-stat-count">{{user_following}}</span> following</li>
</ul>
</div>
Thank you, in advance for any help/suggestions/tips