从字典django / python获取键值 [英] get key value from a dictionary django/python

查看:399
本文介绍了从字典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}))

所以在这种情况下我想打印密钥 alert('{{dict.aa}}'); ie,不使用任何循环,我们可以打印带有aa引用的密钥上面的例子可能有些事情像{{dict ['aa']}}应该给出aa的值

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

推荐答案

不要调用字典 dict ,这将覆盖当前范围内的内置 dict 类型名称。

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 %}

或使用 d.keys 如果你只需要钥匙。

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

这篇关于从字典django / python获取键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆