range in jinja2 inside a for loop

Chandan Gupta picture Chandan Gupta · Jul 17, 2013 · Viewed 77.9k times · Source

I have a nested list. I need to iterate through a list and keep it in for loop as shown below.

{% for alpha in list %}
    <div id="{{ loop.index }}"> 
       <div class='sidebar-one'>
          {% for beta in list[0][2:] %} #I want to iterate through list[0][2:] till list[n][2:]
              <p> {{ beta[0][0] }} </p>
          {% endfor %}
       </div>
    </div>
{% endfor %}

I tried range but no luck.

{% for n in range(1,n) %}
{% for line in check[{{n}}][2:] %}
{% endfor %}

it threw error:

    TemplateSyntaxError: expected token ':', got '}'

Answer

Blender picture Blender · Jul 17, 2013

It's just like Python:

{% for n in range(n) %}
    {% for line in check[n][2:] %}
        <p> {{ beta[0][0] }} </p>
    {% endfor %}
{% endfor %}