[Django]-How to get key value in django template?

67👍

mydict = {'Papa, Joey C': {'10140': 1, '10061': 1, '99214': 1, '99215': 1, '12011': 1, '97606': 1, '49080': 1, '10120': 1, '49440': 1, '49570': 1}, 'Bull, Sherman M': {'99211': 1, '99214': 1, '99215': 1, '99231': 1, '99236': 1, '12051': 1, '15004':1, '47100': 1, '15430': 1, '15431': 1}}

{% for mykey,myvalue in mydict.items %}

    {{ mykey }} : {{ myvalue }}

{% endfor %}
👤lprsd

12👍

Given the dictionary:

{'papa': {'name': 'Papa, Joey C', 'values': {'10140': 1, ...

you can access the values from the keys using {{ mydict1.papa.name }}

Knowing directly use the key in the template if it contains use spaces or specials chars, you could either change your structure (like i just did for the example) or create a custom templatetag/filter which you could use like {{ mydict1|get_key:"Papa, Joey C"}}.

If you want a complete example for a filter just let me know.

1👍

dic_1 is a dictionary. i need the key and value.

we can do loop and get the key and value like this.

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

Leave a comment