Django template how to look up a dictionary value with a variable

后端 未结 8 1604
面向向阳花
面向向阳花 2020-11-22 03:06
mydict = {\"key1\":\"value1\", \"key2\":\"value2\"}

The regular way to lookup a dictionary value in a Django template is {{ mydict.key1 }}

8条回答
  •  轮回少年
    2020-11-22 03:18

    Fetch both the key and the value from the dictionary in the loop:

    {% for key, value in mydict.items %}
        {{ value }}
    {% endfor %}
    

    I find this easier to read and it avoids the need for special coding. I usually need the key and the value inside the loop anyway.

提交回复
热议问题