获取Django模板中for循环的第一个元素

pyl*_*ner 2 django django-templates django-views

模板:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

看法

 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
Run Code Online (Sandbox Code Playgroud)

我想获取 for 循环中的第一个元素

{% for shelf in final_shelf_info %}
Run Code Online (Sandbox Code Playgroud)

并与另一个数据进行比较。

如何获取第一个循环中的第一个元素。

第一个元素:Q004.01.55.01.55.19_9423

{{shelf[0].build}}
我这样试过,没用。

for循环的输出:

1234.xx.xx.xx.xx.xx
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

nik*_*k_m 12

{% for shelf in final_shelf_info %}
    {% if forloop.first %}
        Do something with {{ shelf }} since its the first item iterated
    {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

更多关于文档中{% for %}模板循环。