I currently have the following code in my template:
{% for test in page.object_list %}
<tr>
<td colspan="2" class="testsuite">{{ test.name.0 }}</td>
<td class="testsuite">Failed: {{ percentages.0 }}%</td>
</tr>
{% endfor %}
where test.name.0
is the name of the test suite and percentages
is a list of failed test cases inside that test suite. I was wondering how I might be able to change percentages.0
to something like percentages.i
where i
is incremented on each iteration of the for
loop.
UPDATE
After trying @Gocht's answer I have the following code:
in my template tags/get_percentage.py file
from django import template
register = template.Library()
@register.filter
def get_percentage(percentage_list, i):
return percentage_list[int(i)]
and in my template
{% load get_percentage %}
{% for test in page.object_list %}
<tr>
<td colspan="2" class="testsuite">{{ test.name.0 }}</td>
<td class="testsuite">Failed: {{ percentages|get_percentage: forloop.counter0 }}%</td>
</tr>
{% endfor %}
and am running into a "string index out of range" error. I tried looking for possible causes but none of the answers are related to custom template tags.