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

后端 未结 5 948
梦毁少年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:23

    Just send the template a list of product types and do something like:

    {% for product_type in product_type_list %}
        {{ product_type }}
        {% for product in product_type.products.all %}
            {{ product }}
        {% endfor %}
    {% endfor %}
    

    It's been a little while so I can't remember exactly what the syntax is, let me know if that works. Check the documentation.

提交回复
热议问题