[英]In Django template how to find out if request.user is in a list of objects associated to the user?
I've got a Django model that looks like this 我有一个看起来像这样的Django模型
from django.contrib.auth.models import User
class Cohort(models.Model) :
cohort_name = models.CharField(max_length=64, primary_key=True)
cohort_description = models.TextField(null=False)
creation_date = models.DateTimeField(default=now)
class CohortMembers(models.Model) :
cohort = models.ForeignKey(Cohort)
member = models.ForeignKey(User)
creation_date = models.DateTimeField(default=now)
So you can see there is a many-to-many relationship between Cohorts and Users. 因此,您可以看到同类群组和用户之间存在多对多关系。
In a template, I am listing Cohorts as such (simplified to give you the idea): 在模板中,我这样列出了同类群组(简化后为您提供了想法):
{% for cohort in object_list %}
<a href="./{{ cohort.cohort_name }}/">{{ cohort.cohort_name }}</a>
<!-- list all members of the cohort -->
{% for cohortmember in cohort.cohortmembers_set.all %}
{% if request.user.username == cohortmember.member.username %}
<!-- the user is a member of the cohort, provide a delete button -->
<button>Leave cohort</button>
{% else %}
Some other user called {{ cohortmember.member.username }} is a member.
{% endif %}
{% endfor %}
{% endfor %}
What I need to do, is provide a button for the user to join the cohort if they are not already a member. 我需要做的是提供一个按钮,供用户(如果尚未加入该群组) 加入 。 In the most primitive of python, you might do this; 在最原始的python中,您可以执行此操作;
# clearly some better python would be to use a django model query, rather
# than iterate over the members, but this encapsulates the basic logic
is_member = False
for cohortmember in cohort.cohortmembers_set.all:
if request.user == cohortmember.member:
is_member = True
if is_member :
# a leave button
else:
# a join button
Can I do any of that in the Django template language or do I have to resort to putting code like that into the View class? 我可以使用Django模板语言执行任何操作,还是必须将类似的代码放入View类中?
The only real way I have found to do this is a combination of manipulating the CohortView and a simple bit of template programming. 我发现做到这一点的唯一真实方法是操纵CohortView和简单的模板编程的结合。
I turned the question around: not to ask "is this user a member of that cohort?" 我回避了这个问题:不要问“这个用户是那个群组的成员吗?” but made it into "is this cohort in the list of the user's memberships?" 但是变成“该群组是否在用户的成员资格列表中?” in this simple way. 以这种简单的方式。
In the CohortListView's get_context_data as follows: 在CohortListView的get_context_data中,如下所示:
def get_context_data(self, **kwargs) :
context = super(CohortListView, self).get_context_data(**kwargs)
memberships = CohortMembers.objects.filter(member=self.request.user)
cohorts = []
for membership in memberships:
cohorts.append(membership.cohort)
context['memberships'] = cohorts
return context
Note I'm abandoning the many to many object and just storing a list of Cohorts the user is a member of directly in the context. 注意,我放弃了多对多对象,只存储了用户直接在上下文中所属的同类群组列表。
Then, in the template: 然后,在模板中:
{% for cohort in object_list %}
{% if cohort in memberships %}
<!-- leave button -->
{% else %}
<!-- join button -->
{% endif %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.