[Python] How to get count and show in template - Django

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I was able to get the mutual friends from each users and display each mutual friends image on my template as seen in the attached image to this question. The problem now is that I was not able to get the count, like count and length is not working on template. How can I get it done?

    [​IMG]

    class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=True)
    profile_pic = models.ImageField(upload_to='UploadedProfilePicture/', default="ProfileAvatar/avatar.png", blank=True)
    following = models.ManyToManyField(
    'Profile', # Refers to the User model itself
    symmetrical=False, # If A follows B, B doesn't automatically follow A
    related_name='followers', # Reverse relationship: get followers of a user
    blank=True,
    )


    def FollowingView(request):
    page_title = "Following"

    # All account users
    profile_users = Profile.objects.exclude(
    Q(user=request.user))

    # Mutual Followers
    all_following = request.user.profile.following.values_list('pk', flat=True)
    mutual_followers = Profile.objects.filter(pk__in=all_following)


    Template:

    {% for users in profile_users %}

    # List of users
    <p>{% users.user %}</p>

    {% for user_following in mutual_followers %}
    {% if user_following in users.following.all %}
    <img src="{{ user_following.user.profile.profile_pic.url }}" class="hover-followers-img"/>

    {{ user_following.count }} # Not getting count

    {% endif %}
    {% endfor %}

    {% endfor %}

    Continue reading...

Compartilhe esta Página