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

后端 未结 8 1608
面向向阳花
面向向阳花 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:15

    I had a similar situation. However I used a different solution.

    In my model I create a property that does the dictionary lookup. In the template I then use the property.

    In my model: -

    @property
    def state_(self):
        """ Return the text of the state rather than an integer """
        return self.STATE[self.state]
    

    In my template: -

    The state is: {{ item.state_ }}
    

提交回复
热议问题