2013-05-16 28 views
0

假設我有兩個字符串或整數列表。我想檢查第一個列表中的任何元素是否出現在第二個列表中,並且僅在不滿足該條件時才顯示一次。如果我做for循環兩次,我不會得到預期的結果 - 我想顯示會出現多次哪個項目:Django模板 - 查找列表中是否至少有一個項目出現在其他列表中

# 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 %} 

如何顯示的東西只有一次嗎?這是否是一種檢查這種事情的實用方法?

回答

0

有人可能會說,如果上面的模板屬於所有,但如果不能進行這種比較在一個視圖中使用templatetag

+0

好吧,我看看......會嘗試與templatetag。謝謝! – errata

相關問題