Reputation: 47
I'm trying to count model object using Django but it's not working, it doesn't display anything, that's the HTML code, I've tried both .all.count and .count but both aren't showing anything
<div class="card">
<div> <h3>Followers: {{profiles.follower.all.count}}</h3> </div>
<div> <h3>Followings: {{profiles.following.count}}</h3> </div>
</div>
And that's the python profile function code
def profile(request, username):
user = get_object_or_404(User, username=username)
user_posts= post.objects.filter(user=user)
profiles = Userprofile.objects.filter(user=user)
context = {
'user_posts':user_posts,
'profiles': profiles
}
return render(request, "network/profile.html", context)
**My Models **
class User(AbstractUser):
pass
class post(models.Model):
user = models.ForeignKey('User', on_delete=models.CASCADE, related_name='maker')
text = models.CharField(max_length=255)
date = models.DateTimeField(default=datetime.now())
likes = models.ManyToManyField('User', blank=True)
class Userprofile(models.Model):
user = models.ForeignKey('User', on_delete=models.CASCADE, related_name='profile')
follower = models.ManyToManyField('User', blank=True, related_name='follower')
following = models.ManyToManyField('User', blank=True, related_name='following')
Upvotes: 0
Views: 185
Reputation: 876
add the .first()
in your views.py:
def profile(request, username):
....
user_posts= post.objects.filter(user=user).first()
profiles = Userprofile.objects.filter(user=user).first()
......
Upvotes: 2