Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get key value from a dictionary django/python

How to print the value of a key from the key itself

dict={}
dict.update({'aa':1})
dict.update({'ab':1})
dict.update({'ac':1})
return render_to_response(t.html,  context_instance=RequestContext(request, {'dict':dict}))

So in this case i want to print the key alert('{{dict.aa}}'); i.e,without using any loop can we just print the key with the reference of aa in the the above example may some thing like if {{dict['aa']}} should give the value of aa

like image 873
Rajeev Avatar asked Dec 06 '22 22:12

Rajeev


1 Answers

Never call a dictionary dict, that would overwrite the builtin dict type name in the current scope.

You can access keys and values in the template like so:

{% for item in d.items %}
    key = {{ item.0 }}
    value = {{ item.1 }}
{% endfor %}

or use d.keys if you only need the keys.

like image 154
AndiDog Avatar answered Dec 09 '22 16:12

AndiDog