Use variable as dictionary key in Django template

后端 未结 4 1598
野的像风
野的像风 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:27

    There is a very dirty solution:

    We need d[{{ k }}]
    So here it is: {% for key, value in d.items %} {% if k == key %} {{ value }} {% endif %} {% endfor %}

提交回复
热议问题