How do I get values of the second, third, nth pass in a django template for loop?

时间:2017-04-10 01:17:50

标签: python django

I have the following code in my template:

{% with element=vals|first %} #can use last here to get the last value
    {% for key,value in element.items %}
        <td>{{ value.corr }}</td>
        <td>{{ value.str }}</td>
        <td>{{ value.sig }}</td>
    {% endfor %}
{% endwith %}

That will give me the first values in the first iteration of the loop.

How do I get the second, third, and nth?

I'd thought about using slice, but that doesn't seem to work here -- or I'm not formatting the slice correctly.

Please help!

EDIT: my list of dicts:

[{('age', 'after'): 
        {'str': 'one', 'sig': 'two', 'cor': 'three'}
    }, 
{('age', 'before'): 
        {'str': 'zero', 'sig': 'one', 'cor': 'four'}
    }, 
{('exp', 'after'): 
        {'str': 'one', 'sig': 'two', 'cor': 'three'}
    },   
{('exp', 'before'): 
        {'str': 'zero', 'sig': 'one', 'cor': 'four'}
}]

EDIT 2: Desired output

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Parameters</th>
            <th>Pos/Neg</th>
            <th>Str</th>
            <th>Sig</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">First Table Row</th>
                {% with element=q3vals|first %}
                    {% for key,value in element.items %}
                        {% if forloop.counter == 1 %}
                            <td>{{ value.corr }}</td>
                            <td>{{ value.str }}</td>
                            <td>{{ value.sig }}</td>
                        {% endif %}
                    {% endfor %}
                {% endwith %}
        </tr>
        <tr>#second loop through the list of dicts</tr>
        <tr>#third loop through the list of dicts
        <tr>
            <th scope="row">Fourth Table Row</th>
                {% with element=q3vals|last %} #last loop through
                    {% for key,value in element.items %}
                        <td>{{ value.corr }}</td>
                        <td>{{ value.str }}</td>
                        <td>{{ value.sig }}</td>
                    {% endfor %}
                {% endwith %}      
        </tr>
    </tbody>
</table>

2 个答案:

答案 0 :(得分:3)

您可以使用forloop.counter

{% with element=vals|first %} {# can use last here to get the last value #}
    {% for key,value in element.items %}
        {% if forloop.counter == 2 %}  {# or if forloop.first etc. #}
            <td>{{ value.corr }}</td>
            <td>{{ value.str }}</td>
            <td>{{ value.sig }}</td>
        {% endif %}
    {% endfor %}
{% endwith %}

[编辑]:

{% for dict in q3vals %}
    {% for key, inner_dict in dict.items %}
    {# key, each time, will be ('age', 'after'), ('age', 'before') etc #}
    {# inner_dict, each time, will be {'str': 'one', 'sig': 'two', 'cor': 'three'} etc #}
        <tr>
            <th scope="row">Table Row #{{ forloop.counter }}</th>
            <td>{{ inner_dict.cor }}</td>
            <td>{{ inner_dict.str }}</td>
            <td>{{ inner_dict.sig }}</td>
        </tr>
    {% endfor %}
{% endfor %}

答案 1 :(得分:0)

Refer to official doc (https://docs.djangoproject.com/en/1.11/ref/templates/language/#variables), you need to try the for loop like this:

{% for k, v in defaultdict.iteritems %}
    Do something with k and v here...
{% endfor %}

Hence, your code should be:

 {% for key,value in element.iteritems %}

Hope it will help.