I am building a small social media website using Django, When I Click on follow button, I get this error saying: “User matching query does not exist.” And showing the error here in the profile view function”user_object = User.objects.get(username=pk)
this is the follow function in view.py file,
def Follow(request):
if request.method == "POST":
follower_username = request.POST['follower']
user_username = request.POST['user']
# Get User model instances based on usernames
follower_user = User.objects.get(username=follower_username)
user_user = User.objects.get(username=user_username)
if FollowersCount.objects.filter(follower=follower_user,user=user_user).first():
delete_follower = FollowersCount.objects.get(follower=follower_user,user=user_user)
delete_follower.delete()
else:
add_follower = FollowersCount.objects.create(follower=follower_user,user=user_user)
add_follower.save()
return redirect('profile',username=follower_user)
I tried to give try and except to the user_object, but it didn’t work this is the profile view function,
def Profile_view(request,pk):
user_object = User.objects.get(username=pk)
user_profile = Profile.objects.get(user=user_object)
user_post = Post_Upload.objects.filter(user=user_object)
following_count = FollowersCount.objects.filter(follower=user_object).count()
followers_count = FollowersCount.objects.filter(user=user_object).count()
post_count = user_post.count()
context = {
"user_profile":user_profile,
"user_post":user_post,
"post_count":post_count,
"following_count":following_count,
"followers_count":followers_count,
}
return render(request, 'profile.html',context)