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

我希望每种语言只看一次,但我得到了两次。

4

2 回答 2

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 %}
于 2012-05-23T05:23:48.887 回答
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 %}
于 2012-05-23T05:38:47.873 回答