2012-05-23 43 views
0
from django.template import Template, Context 
template = Template(""" 
     {% for language in languages %} 
     THIS IS forloop.parentloop.first +++++++++ 
     {% for tag in revision_tags %} 
      {% for case in CASES %} 
      <tr> 
      {% if forloop.parentloop.first %}<th rowspan="6">{{ language|capfirst }}</th>{% endif %} 
      {% if forloop.first %}<th rowspan="2">{{ tag }}</th>{% endif %} 
      <th>{{ case|capfirst }}</th> 
      </tr> 
      {% endfor %} 
     {% endfor %} 
     {% endfor %} 
""") 

c = Context({ 
    'languages': ["english", "french"], 
    'revision_tags': ["d", "s", "p"], 
    'CASES': ["foo", "bar"], 
}) 

h = template.render(c) 
print h 

我希望看到每種語言只有一次,但我得到他們兩次。這是django模板re:forloop.parentloop.first中的錯誤還是我誤解?

回答

0

這是不是一個錯誤,我誤解了。測試應該是{% if forloop.parentloop.first and forloop.first %},在這種情況下,每個盛大父代迭代獲得1個真實值,即language

{% for language in languages %} 
    {% for tag in revision_tags %} 
     {% for case in CASES %} 
     <tr> 
     {% if forloop.parentloop.first and forloop.first %} 
     <th rowspan="6">{{ language|capfirst }}</th> 
     {% endif %} 
     {% if forloop.first %}<th rowspan="2">{{ tag }}</th>{% endif %} 
     <th>{{ case|capfirst }}</th> 
     </tr> 
     {% endfor %} 
    {% endfor %} 
    {% endfor %} 
0

試試這個(未測試)...

{% for language in languages %} 
    THIS IS forloop.parentloop.first +++++++++ 
    {% for tag in revision_tags %} 
     {% if forloop.parentloop.first %}<th rowspan="6">{{ language|capfirst }}</th>{% endif %} 
     {% for case in CASES %} 
     <tr> 
     {% if forloop.first %}<th rowspan="2">{{ tag }}</th>{% endif %} 
     <th>{{ case|capfirst }}</th> 
     </tr> 
     {% endfor %} 
    {% endfor %} 
    {% endfor %} 
相關問題