django在listview模板中循环

时间:2013-10-21 14:44:04

标签: django python-2.7 django-templates

我对django和python相对较新,我最近在模板的列表视图中遇到for loop问题。 因此,我生成一个vanilla列表视图,然后将data添加到我的上下文处理器。 data看起来像这样:

data = [
({'map_b': u'britney'}, {'map_s': u'rock'}, {'map_u': u'http://bhc.com/'}), 
({'map_b': u'britney'}, {'map_s': u'pop'}, {'map_u': u'http://hjgjkpop.com'})
({'map_b': u'britney'}, {'map_s': u'sport'}, {'map_u': u'http://hjgjksp.com'})
({'map_b': u'britney'}, {'map_s': u'default'}, {'map_u': u'http://hjgjkde.com'})
({'map_b': u'kate perry'}, {'map_s': u'rock'}, {'map_u': u'http://kpbhc.com/'}), 
({'map_b': u'kate perry'}, {'map_s': u'pop'}, {'map_u': u'http://kphjgjkpop.com'})
({'map_b': u'kate perry'}, {'map_s': u'sport'}, {'map_u': u'http://kphjgjksp.com'})
({'map_b': u'kate perry'}, {'map_s': u'default'}, {'map_u': u'http://kphjgjkde.com'})
({'map_b': u'bon jovi'}, {'map_s': u'default'}, {'map_u': u'http://bjkphjgjkde.com'})
]

现在,在我的模板中,我想从列表视图中检查上述data的值与我object_list上的值。为此,我做了类似的事情:

{% for i in object_list %} # i contants i.b, i.s and i.o

    {% for b, s, u in data %}
        {% if b.map_b|lower in i.b|lower and s.map_s|lower in i.s|lower %}
    <a class="lead" href="{{u.map_u}}" rel="nofollow">
        [{{i.b}}] {{i.s|title|truncatechars:100}}
    </a>
    <h5># Fun Fact: {{ i.o|truncatechars:100 }} @ {{i.b}}</h5>
        {% endif %}
    {% endfor %}

{% endfor %}

所以,我要做的是,检查if b.map_b is in i.bif s.map_s in i.s,然后显示u.map_u。它工作正常,但我想添加的是一个附加条件,如果它无法获取b.map_b and s.map_s to match i.b and i.s,则显示与u.map_u对应的i.b and "default"简而言之,我想使用“默认”map_u值无法找到上述内容。

我无法看到如何实现这一目标 - 我将不胜感激任何指导。

我希望我能很好地解释这个问题 - 但也许不是:(

0 个答案:

没有答案