Use variable as dictionary key in Django template

后端 未结 4 1612
野的像风
野的像风 2020-11-27 04:23

I\'d like to use a variable as an key in a dictionary in a Django template. I can\'t for the life of me figure out how to do it. If I have a product with a name or ID field,

4条回答
  •  心在旅途
    2020-11-27 04:49

    You need to prepare your data beforehand, in this case you should pass list of two-tuples to your template:

    {% for product, rating in product_list %}
        

    {{ product.name }}

    {{ rating }}

    {% endfor %}

提交回复
热议问题