range in jinja2 inside a for loop

PythonLoopsJinja2

Python Problem Overview


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 '}'

Python Solutions


Solution 1 - Python

It's just like Python:

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

Solution 2 - Python

You can use the "length" property:

{% for n in range(yourList| length) %}
       <p class="someclass">{{n + 1}}.</p>
       <a class="someclass2" 
       href="{{ url_for( 'yourFunction', Int = yourList[n].iterable)}}">
       {{yourList[n].iterable}}</a><br>
{% endfor %}

Length is similar to len(yourlist) that we have in python.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChandan GuptaView Question on Stackoverflow
Solution 1 - PythonBlenderView Answer on Stackoverflow
Solution 2 - PythonDinidinizView Answer on Stackoverflow