I'm new to Django and I'm trying to create a project similar to a social media site, where users can go to a user profile, and 'follow' another user profile. When I click 'follow' on the profile, currently I don't get any errors at all. Nothing happens. I don't see anything in my console/terminal etc. There's no error. Help is appreciated!
views.py
def fol(request, username):
if request.method == 'GET':
currentuser = request.user
profileuser = get_object_or_404(User, username=username)
posts = Post.objects.filter(user=profileuser).order_by('id').reverse()
follower = Follow.objects.filter(target=profileuser)
following = Follow.objects.filter(follower=profileuser)
following_each_other = Follow.objects.filter(follower=currentuser, target=profileuser)
totalfollower = len(follower)
totalfollowing = len(following)
paginator = Paginator(posts, 10)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'posts': posts.count(),
'profileuser': profileuser,
'follower': follower,
'totalfollower': totalfollower,
'following': following,
'totalfollowing': totalfollowing,
'followingEachOther': following_each_other
}
return render(request, "network/profile.html", context)
else:
currentuser = request.user
profileuser = get_object_or_404(User, username=username)
posts = Post.objects.filter(user=profileuser).order_by('id').reverse()
following_each_other = Follow.objects.filter(follower=currentuser, target=profileuser)
paginator = Paginator(posts, 10)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
if not following_each_other:
f = Follow.objects.create(target=profileuser, follower=currentuser)
f.save()
follower = Follow.objects.filter(target=profileuser)
following = Follow.objects.filter(follower=profileuser)
following_each_other = Follow.objects.filter(follower=request.user, target=profileuser)
totalfollower = len(follower)
totalfollowing = len(following)
context = {
'posts': posts.count(),
'profileuser': profileuser,
'page_obj': page_obj,
'follower': follower,
'following': following,
'totalfollowing': totalfollowing,
'totalfollower': totalfollower,
'followingEachOther': following_each_other
}
return render(request, "network/profile.html", context)
else:
following_each_other.delete()
follower = Follow.objects.filter(target=profileuser)
following = Follow.objects.filter(follower=profileuser)
totalfollower = len(follower)
totalfollowing = len(following)
context = {
'posts': posts.count(),
'profileuser': profileuser,
'page_obj': page_obj,
'follower': follower,
'following': following,
'totalfollowing': totalfollowing,
'totalfollower': totalfollower,
'followingEachOther': following_each_other
}
return render(request, "network/profile.html", context)
urls.py
urlpatterns = [
path("", views.index, name="index"),
path("fol/<str:username>", views.fol, name="fol"),
path("following_list/<str:username>", views.following_list, name='following_list'),
path("profile/<str:username>", views.profile, name="profile"),
]
html file for the profile:
{% extends "network/layout.html" %}
{% load static %}
{% block body %}
{% if message %}
<h4 class="message">{{message}}</h4>
{% endif %}
<h3> You are viewing {{ profileuser.username }}'s profile. </h3> <br>
<br>
{% if user1 != user2 %}
<div class="pull-right">
<a href="{% url 'fol' username=profileuser.username %}"></a><input class="btn btn-primary"
type="submit" value="Follow">
</div>
<div class="pull-right">
<input class="btn btn-primary" type="submit" value="Unfollow">
</div>
{% endif %}
{% for i in page_obj %}
<div class='card mb-3' style="max-width: 530px;" id="card-posts">
<div class="row no-gutters">
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title"><a href="{% url 'profile' username=i.username %}">.
{{i.username}}</a></h5>
<p class="card-text">{{i.text}}</p>
<p class="card-text">{{i.timestamp}}</p>
<p class="card-text">{{i.amount_likes}}</p>
<div class="like mt-3">
<img
data-id="{{i.id}}"
id="post-like-{{i.id}}"
class="liked"
{% if not request.user in i.like.all %}
data-is_liked="no"
src="https://img.icons8.com/emoji/452/white-heart.png"
{%else%}
data-is_liked="yes"
src="https://img.icons8.com/emoji/452/red-heart.png"
{%endif%}
/>
<span id="post-count-{{i.id}}">{{i.like.count}}</span>
</div>
<br>
{% if user1 == user2 %}
<span class="text-primary edit" data-id="{{i.id}}" id="edit-btn-{{i.id}}">Edit</span>
<br><br>
{% endif %}
<span id="post-content-{{i.id}}" class="post" >{{i.post}}</span>
<textarea data-id="{{i.id}}" id="post-edit-{{i.id}}" style="display:none;" class="form-control textarea" row="3">{{i.post}}</textarea>
</div>
</div>
</div>
</div>
{% endfor %}
<br><br>
<div class="container">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}"
class="page-link">Previous</a>
{% else %}
<li class="page-item disabled"><a class="page-link">Previous</a></li>
{% endif %}
{% if page_obj.number %}
<a class="page-link">{{ page_obj.number }}</a>
{% else %}
<a class="page-link">0</a>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}" class="page-link">Next</a>
{% else %}
<a class="page-link">Next</a>
{% endif %}
</ul>
</div>
{% endblock %}
models.py
class User(AbstractUser):
pass
class Post(models.Model):
text = models.TextField(max_length=500, blank=True, null=True)
username = models.ForeignKey('User', on_delete=models.CASCADE, related_name='author',
null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
like = models.ManyToManyField(
User, blank=True, related_name="liked_user")
def __str__(self):
return self.user.username
class Follow(models.Model):
target = models.ForeignKey('User', on_delete=models.CASCADE, related_name='followers')
follower = models.ForeignKey('User', on_delete=models.CASCADE, related_name='targets')