问题描述
假设我有一个查询:
cities = City.objects.all()
在我的模板中,我已经完成:
In my template I have done:
{% for city in cities %}
{{city.friend_name}}
<a href="{% url "my_url" city.friend_name.id %}" class="btn btn-primary">View Detail</a>
{% endfor %}
它给我4个ID为alex 1,matt 2,mack 3,mack 3的朋友的名字.但是这里重复了mack.我只想要麦克.如果重复这些值,我希望只打印一次.
It gives me name of 4 friends with id say alex 1, matt 2, mack 3, mack 3. But here mack is repeated. I only want mack once. If the values are repeated I want it to be printed only once.
如何在模板中执行此操作.我的意思是有类似 {{city.friend_name | distinct}}
之类的东西
How can I do this in template. I mean is there something like {{city.friend_name|distinct}}
or something else
我不想要独特的城市.我希望朋友在城市上的名字唯一.
I dont want unique city. I want friends name on city to be unique.
谢谢
推荐答案
为什么要在模板中执行此操作?尝试修改 cities
本身,使其具有唯一的条目.您可以将您的 list
转换为 set
,然后重新将其转换回:
Why do you want to do it in the template? Try to modify cities
itself so that it has unique entries.You could convert your list
to a set
and then re-convert it back:
cities_unique = list(set(cities))
如果必须显示唯一属性,请使用重新组合功能
If you have to show unique attributes, use the regroup feature
这篇关于django模板仅显示不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!