2012-05-23 44 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 %} 
相关问题