0

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.

DjangoNoob
  • 157
  • 2
  • 2
  • 8
  • I don't see any `i`, but if `percentages` is inside `test` why don't you call `test.percentages.0`? – Gocht Jun 02 '15 at 21:27
  • I just made up `i`. In regular python code I would just say `percentages[i]` and increment `i` after that line. Also, `percentages` is not in `test`. My original thought was to add a `percent_failed` attribute in the model, but I believe the guy who wrote this project is populating those model objects with parsed data from an XML file and since the XML file doesn't have the number of failed tests the app crashes. So I am just calculating the percentages inside my views.py file and then passing it into the page – DjangoNoob Jun 02 '15 at 21:44
  • That is an `IndexError`, make sure you list has the correct length. – Gocht Jun 02 '15 at 23:09
  • I have already verified that it is the correct length – DjangoNoob Jun 02 '15 at 23:43
  • That error is a result of attempting to index the empty string. That is the only time using 0 as the index would cause that. Look at your data and serif one of the test names is the empty sting – metahamza Jun 03 '15 at 03:46

1 Answers1

1

I will assume you missed in your question percentages.0 instead percentages.i. In a for on template, you can access the index with forloop variable. Like this:

{% for test in page.object_list %}
    {{ forloop.counter }}   # This will start at 1
    {{ forloop.counter0 }}  # This will start at 0
    # do something here
{% endfor %}

See: this answer

Community
  • 1
  • 1
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • Okay, but how can I use that to index my percentages list? I tried `percentages.forloop.counter0` and it didn't work – DjangoNoob Jun 02 '15 at 22:00
  • Well, that's not possible, but there are other solutions. You could do a second for and compare its counters, or you could create a custom template filter. I found this for you: [custom filter](http://stackoverflow.com/a/29664945/3945375) and [for inside for](http://stackoverflow.com/a/6963594/3945375) and some documentation about this: [for documentation](https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#for) – Gocht Jun 02 '15 at 22:21
  • Ahh okay, I think I get it. So in the link you sent me about the custom filter I would use `{{ percentages|index:forloop.counter0 }}` inside my template after loading index in – DjangoNoob Jun 02 '15 at 22:31
  • Yep, that's the idea, but you need to create and register the index filter first. – Gocht Jun 02 '15 at 22:33
  • Okay, I tried it but am getting a "string index out of range" error. I updated my post with the code I tried. Any idea why it's causing that? – DjangoNoob Jun 02 '15 at 23:03