在模板中迭代dics列表

时间:2013-11-25 14:25:23

标签: python templates django-templates tornado

clients_list

   {'clients': [
    {'id': 357995, 'value': 1.0}, 
    {'id': 369743, 'value': 0.9}
    ]}

{% try %}
{% if clients_list %}
{% for client in clients_list %}
    {% for user in client %}
        {% raw user.id %}
        {% raw user.value %}
    {% end %}
{% end %}
{% end %}
{% except %}
{% end %}

预期输出:

357995
1.0

369743
0.9

问题是模板中的循环是错误的。我如何访问id和值?

这是一个龙卷风模板,但我认为这与django类似。

更新

{% try %}
{% if clients_list %}
{% for client in clients_list %}
    {% raw client %} // outputs the clients_list
    {% for user in client %}
        {% raw user %} outputs 'clients'
    {% end %}
{% end %}
{% end %}
{% except %}
{% end %}

1 个答案:

答案 0 :(得分:1)

这是解决方案。

{% try %}
{% if clients_list %}
{% for client in clients_list %}
    {% for user in client['clients'] %}
        {% raw user['id'] %}
        {% raw user['value'] %}
    {% end %}
{% end %}
{% end %}
{% except %}
{% end %}