Home > Back-end >  Display all User profiles, in Django template, liked by the currently logged-in User (under ManytoMa
Display all User profiles, in Django template, liked by the currently logged-in User (under ManytoMa

Time:06-19

I built a portal, where members can see other users' profiles and can like them. I want to show a page where the currently logged-in users can see a list of profiles only of the members they liked.

The Model has a filed 'liked', where those likes of each member profile are stored:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    company = models.CharField(max_length=500, blank = True)
    city = models.CharField(max_length=100, blank = True)
    website = models.URLField(max_length=500, blank = True)
    liked = models.ManyToManyField(User, related_name='user_liked', blank=True)

My views.py, and here I only show all members so on my template I can loop through each member in members... Including 'member.profile' details from the Profile model.

@login_required
def all_fav_members(request):
    users = User.objects.all
    context = {'members':users}
    return render(request, 'club/all_fav_members.html', context)

I've tried many things, both under views.py and my HTML template, but I was not able to loop through all users associated with a specific Profile under the 'liked' field where that user is equal to request.user.

I'm new to Django, hence trying multiple things. The outcome usually is I get the whole list of members, not the ones current user liked. One of the not working examples:

{% if member.profile.liked.filter(id=request.user.id).exists()%}

My template:

 {% for member in members %} 
     <table >
        <thead>
           <tr id="header-paragraph-table-top">
               <th>Name & Surname</th>
               <th>Email</th>
               <th>Company</th>
           </tr>
        </thead>
        <tbody>
           <tr id="paragraph-table">
               <td>{{ member.first_name|capfirst }} {{ member.last_name|capfirst }}</td>
               <td><a href="mailto:{{ member.email }}">{{ member.email }}</a></td>
               <td>{{ member.profile.company }}</td>
           </tr>
        </tbody>
     </table>

urls.py

path('all_fav_members/', views.all_fav_members, name='all_fav_members'),

CodePudding user response:

I would probably use template tags to solve this issue. Read this page to get to know how to register template tags: https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/

Inside your_template_tags:

from django import template

register = template.Library()

@register.filter
def liked_user(user, other_user):
    return user.profile.liked.filter(id=other_user.id).exists()

Inside your template you could do the following:

{% load your_template_tags %}
{% if member|liked_user:request.user %}

Although I would probably handle it in the views.py like this:

for member in context["members"]:
    member.liked_by_user = member.profile.liked.filter(id=request.user.profile.id).exists()

Then you could just use this property in your template like:

{% if member.liked_by_user %}
  • Related