如何在
django模板中访问字典值?我想实际得到变量a的值
class Emp(models.Model): name = models.CharField(max_length=255,unique=True) address1 = models.CharField(max_length=255) def get_names(self): names = {} names_desc = {} nbl = {} names.update({'a' : 1}) names_desc.update({'b' : 2}) nbl.update({'names' : names,'names_desc' : names_desc}) return nbl
emp是我传递给模板的对象
是{{emp.get_names}} ??或{{emp.get_names.names.a}}
解决方法
{{ emp.get_names.names.a }} will get you 1 in the template {{ emp.get_names.names }} will get you {'A':1} in the template {{ emp.get_names }} will get you {'names_desc': {'b': 2},'names': {'a': 1}} in the template