Django - How to do tuple unpacking in a template 'for' loop

后端 未结 5 949
梦毁少年i
梦毁少年i 2020-12-13 05:57

In my views.py, I\'m building a list of two-tuples, where the second item in the tuple is another list, like this:

[ Product_Type_1, [ product_1, product_2 ]         


        
5条回答
  •  我在风中等你
    2020-12-13 06:29

    Another way is as follows.

    If one has a list of tuples say:

    mylst = [(a, b, c), (x, y, z), (l, m, n)]
    

    then one can unpack this list in the template file in the following manner. In my case I had a list of tuples which contained the URL, title, and summary of a document.

    {% for item in mylst %}    
         {{ item.0 }} {{ item.1}} {{ item.2 }}    
    {% endfor %}
    

提交回复
热议问题