2017-03-20 49 views
0

模板:獲得的第一個元素在爲Django模板循環

{% 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 %} 

查看

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 

我想獲得的第一個元素在for循環

{% for shelf in final_shelf_info %} 

並與其他數據進行比較。

如何獲得第一個循環中的第一個元素。

第一個元素:Q004.01.55.01.55.19_9423

{{貨架[0] .build}}
我試過這樣的,它沒有工作。

的for循環的輸出:

1234.xx.xx.xx.xx.xx 

任何有助於將不勝感激。

回答

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

更多關於{% for %}模板循環在docs

+0

非常感謝@nik_m。你是頂尖人物;) – pydev

0
{% if final_shelf_info.0 == shelf %} 

{% if final_shelf_info.first == shelf %} 
1

你可以做這樣的事情:

 {% for t in things %} 

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

       // do stuff 

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

     {% endfor %} 

更多的文檔,請Django documentation

+0

非常感謝@ user4426017的幫助。 – pydev