[英]Display all User profiles, in Django template, liked by the currently logged-in User (under ManytoMany field)
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.我的views.py ,在这里我只显示所有成员,所以在我的模板上我可以遍历成员中的每个成员...包括来自 Profile 模型的“member.profile”详细信息。
@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.我在views.py和我的HTML模板下尝试了很多东西,但是我无法遍历与“喜欢”字段下的特定配置文件相关联的所有用户,该用户等于request.user。
I'm new to Django, hence trying multiple things.我是 Django 的新手,因此尝试了多种方法。 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 class="table w-100 table-hover">
<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网址.py
path('all_fav_members/', views.all_fav_members, name='all_fav_members'),
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/阅读此页面以了解如何注册模板标签: https ://docs.djangoproject.com/en/4.0/howto/custom-template-tags/
Inside your_template_tags:在 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:虽然我可能会像这样在views.py中处理它:
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 %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.