How to unzip multiple variables on Jinja2

I am trying to unzip a few variables on the jinja engine. How can I achieve this?

I am trying to achieve something like this:

{% for item1, item2, item3 in items %} <div class="row"> <div class="four columns"> <img src="static{{ item1.pics.0 }}" class="picitem" alt=""/> </div> <div class="four columns"> <img src="static{{ item2.pics.0 }}" class="picitem" alt="" /> </div> <div class="four columns"> <img src="static{{ item3.pics.0 }}" class="picitem" alt=""/> </div> </div> {% endfor %} 

This clearly does not work, giving;

 ValueError: too many values to unpack 

Any ideas would be appreciated.

+4
source share
2 answers

Use the batch filter to iterate over the pieces:

 {% for tmp in items|batch(3) %} <div class="row"> {% for item in tmp %} <div class="four columns"> <img src="static{{ item.pics.0 }}" class="picitem" alt=""/> </div> {% endfor %} </div> {% endfor %} 
+6
source

You need to restore your β€œitems” in order to unpack.

eg:

 item1 = [1,2,3] item2 = [a,b,c] item3 = [11,22,33] items = zip(item1, item2, item3) 

Send this to the template. Hope this helps.

0
source

Source: https://habr.com/ru/post/1490217/


All Articles