Django模板 - 查找列表中是否至少有一个项目出现在其他列表中

时间:2013-05-16 11:51:24

标签: python django for-loop django-templates

假设我有两个字符串或整数列表。我想检查第一个列表中的任何元素是否出现在第二个列表中,并且如果不满足该条件,则仅显示一些内容。如果我for循环两次,我将无法获得所需的结果 - 我想要显示的项目将多次显示:

# I send this from view to template

b = [{'id':1}, {'id':2}, {'id':3}, {'id':4}, {'id':5}]
d = [{'id':5}, {'id':6}, {'id':7}, {'id':8}]

# In template

{% for a in b %}
  {% for c in d %}
    {% if not a.id == c.id %}
      this will be displayed multiple times
    {% endif %}
  {% endfor %}
{% endfor %}

我怎样才能在这里展示一次?这是一种检查这类事情的实用方法吗?

1 个答案:

答案 0 :(得分:0)

如果上述内容完全属于模板,可以争论,但如果您无法在视图中执行此比较,请使用templatetag