【问题标题】:Django templates - find if at least one item from a list appears in other listDjango模板 - 查找列表中的至少一项是否出现在其他列表中
【发布时间】:2013-05-11 07:12:03
【问题描述】:

假设我有两个字符串或整数列表。我想检查第一个列表中的任何元素是否出现在第二个列表中,如果不满足该条件,则仅显示一次。如果我执行 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 %}

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

【问题讨论】:

    标签: python django for-loop django-templates


    【解决方案1】:

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

    【讨论】:

    • 好的,我明白了...将尝试使用模板标签。谢谢!
    猜你喜欢
    • 2020-05-11
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多