我在我的模板中访问特定键时遇到了一些问题。这里是我的代码:django - 使用循环变量作为字典键
{% for rsim,couples in zipped_formated_rxnsim_pairedsim %}
-{{rsim.xxx_fk.xxx_code_char}} #ex : toto
{% for rsim2,couples2 in mydict.{{rsim.xxx_fk.xxx_code_char}}%}
<li> {{rsim2}} - {{couples2}}</li>
{% endfor %}
{% endfor %}
它给了我这个错误,
无法解析余: '{{rsim.xxx_fk.xxx_code_char}}' 从“mydict {{rsim.xxx_fk .xxx_code_char}}”
我不理解这种行为,因为如果我硬代码行{% for rsim2,couples2 in mydict.{{rsim.pdb_fk.pdb_code_char}}%}
到{% for rsim2,couples2 in mydict.toto %}
它的工作......我的意思是,我怎么能做些什么概念呢?
我真的需要这种循环结构,是因为我想通过“rsim2,couples2”元组只对特定的“RSIM,情侣”元组。换言之,“rsim2,couples2”是与“rsim,夫妇”链接的额外数据。比使用密钥的字典更好的解决方案是什么?
我尝试使用{{with}}标记,添加一个变量直接作为键使用,但没有奏效。任何帮助,将不胜感激。
谢谢!
有什么不明白的?如果您已经在模板标签中,则不能使用“{{}}”变量语法。 –
通过不理解,我的意思是,我该怎么做类似的事情。谢谢@DanielRoseman –
[Django模板如何用变量查找字典值]的可能重复(http://stackoverflow.com/questions/8000022/django-template-how-to-look-up-a-dictionary -value-with-a-variable) –