我有一个django模板,我想用它来过滤查询集中的对象。我有一个组,我想添加新成员。我有一个成员对象列表,用于跟踪创建组时添加的组成员。通过显示用户朋友列表添加成员,其名称旁边有一个复选框。我现在想要显示一个尚未成为该组成员的朋友列表,然后将它们添加到该组中(如果已选中)。我遇到的问题是我创建的html模板过滤系统只显示了尚未成员的朋友列表...任何人都可以帮我解决这个问题。我有以下所有代码:
这是传递的查询:
# grab the group members
members = Member.objects.filter(group = group).all()
# grab all of the friends fo the logged in user
friender = Friend.objects.filter(user = user.username).all()
friended = Friend.objects.filter(friend = user).all()
friends = friender | friended
# the required parameters for this form
parameters = {
'friends':friends,
'members':members,
'group':group,
'message':message,
}
return render(request, 'groups/add_members.html', parameters)
现在这里是html模板:
{% extends "general/header.html" %}
{% block content %}
<h1>Create Group</h1>
<p>{{ message }}</p>
<form method="POST">
{% csrf_token %}
{% for friend in friends %}
{% for member in members %}
{% if member.user.username != friend.friend.username %}
{% if member.user.username != friend.user %}
{% if friend.user == user.username %}
<p>
{{ friend.friend.username }}
<input type="checkbox" name="{{friend.friend.username}}" value="{{friend.friend.username}}">
</p>
{% endif %}
{% if friend.friend == user %}
<p>
{{ friend.user }}
<input type="checkbox" name="{{friend.user}}" value="{{friend.user}}">
</p>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
以下是现在存在的朋友和成员列表
logged in user omar
friends: hani, assad, rana, amer
members: hani, assad
这是显示的内容:
以下是模型:
# the following are models for accepted friend requests
class Friend(models.Model):
user = models.CharField(max_length=22, default='current user')
friend = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
created = models.DateTimeField(auto_now_add=True)
class UserActivity(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
account = models.CharField(max_length=150, null=True)
expense = models.ForeignKey(Expense, null=True, on_delete=models.CASCADE)
request = models.ForeignKey(Request, null=True, on_delete=models.CASCADE)
description = models.CharField(max_length=200, default='some action')
reference = models.IntegerField(default = '101', null = True)
category = models.IntegerField(default=1)
status = models.SmallIntegerField(default=1)
# 1 = unseen
# 2 = seen
created = models.DateTimeField(auto_now_add=True)
# Member stores all of the different members to each of the created groups
class Member(models.Model):
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
group = models.ForeignKey(Group, default=1 , on_delete=models.CASCADE)
status = models.SmallIntegerField(default=1)
# 1 = member
# 2 - host
created = models.DateTimeField(auto_now_add=True)
答案 0 :(得分:1)
没有模型很难调试,但你可以像这样简化你的代码:
创建成员列表:
members = [member.user.username for member in Member.objects.filter(group = group)]
将HTML模板更改为此。您检查friend
列表中是否有members
以及True
是否显示了他:
{% extends "general/header.html" %}
{% block content %}
<h1>Create Group</h1>
<p>{{ message }}</p>
<form method="POST">
{% csrf_token %}
{% for friend in friends %}
{% if friend.friend.username not in members %}
<p>
{{ friend.friend.username }}
<input type="checkbox" name="{{friend.friend.username}}" value="{{ friend.friend.username }}">
</p>
{% endif %}
{% if friend.user not in members %}
<p>
{{ friend.user }}
<input type="checkbox" name="{{friend.user}}" value="{{ friend.user }}">
</p>
{% endif %}
{% endfor %}
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
这将仅显示当前不是该组成员的用户。
其他评论:
.all()
。您已经过滤了,根据定义,并非所有过滤。