3

Template:

{% for code in group_codes %}
        *_{{ code.build }}_*<br />
        {% if test_info.test_type = 0 %}
            {{ code.pre_testing_fail }}/{{ code.pre_testing_total }} failed pre-test<br />
        {% else %}

        {% for shelf in final_shelf_info %}

        {{ shelf.build }} <br/>

           {% if shelf.build = code.build %}

            {{ mr_script_count_func }}/{{ code.script_total }} 
            <span>MR</span> failed during script<br />
            {{gw_script_count_func}}/{{ code.script_total }} 
            <span>GW</span> failed during script<br />
            {{ mr_post_count_func }}/{{ code.post_testing_total }} 
            MR failed during post-test<br/>
            {{ gw_post_count_func }}/{{ code.post_testing_total }}
             GW failed during post-test<br/>
             {% endif %}

        {% endfor %}


        <br/>
        <br/>
    {% endif %}
{% endfor %}

View

 def final_shelf_info(self):
    shelves = self.bugs_stbs()
    shelfList = list()

    for shelf in shelves:
        shelfList.append(shelf.results_stb_id)

    final_info = ResultsStbs.objects.select_related(
        'build',
        'pre_testing_result',
        'script_result',
        'post_result',
    ).filter(
        results_stb_id__in=shelfList,
        tr_test_case_id=self.kwargs['trTestCaseID'],
    ).order_by(
        'pair_no','shelf_no',
    )

    for info in final_info:
        if info.stb_hw_info_ids:
            info.stb_type = info.stb_hw_info_ids.stb_hw_info.stb_type
        else:
            info.stb_type = None

    return final_info

I would like to get the first element in the for loop

{% for shelf in final_shelf_info %}

and compare with another data.

How can I get the first element in the first loop.

First element : Q004.01.55.01.55.19_9423

{{ shelf[0].build }}
I tried like that, it did not work.

The output of the for loop:

1234.xx.xx.xx.xx.xx

Any helps would be appreciated.

pylearner
  • 537
  • 2
  • 8
  • 26

3 Answers3

16
{% for shelf in final_shelf_info %}
    {% if forloop.first %}
        Do something with {{ shelf }} since its the first item iterated
    {% endif %}
{% endfor %}

More on the {% for %} template loop in the docs.

nik_m
  • 11,825
  • 4
  • 43
  • 57
3

You could do something like this:

        {% for t in things %}

                {% if forloop.first %}
                    // do something                            
                {% endif %}

                // do stuff

                {% if forloop.last or things.count == 1 %}
                    // do something
                {% endif %}

        {% endfor %}

More documentation is available at Django documentation

user4426017
  • 1,930
  • 17
  • 31
0
{% if final_shelf_info.0 == shelf %}

or

{% if final_shelf_info.first == shelf %}
iklinac
  • 14,944
  • 4
  • 28
  • 30