我的视图代码基本上看起来像这样:
context = Context()
context['my_dict'] = {'a': 4, 'b': 8, 'c': 15, 'd': 16, 'e': 23, 'f': 42 }
context['my_list'] = ['d', 'f', 'e', 'b', 'c', 'a']
Run Code Online (Sandbox Code Playgroud)
我想在Django模板中做的是:
<ul>
{% for item in my_list %}
<li>{{ item }} : {{ my_dict.item }}</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
我希望这输出:
<ul>
<li> d : 16 </li>
<li> f : 42 </li>
<li> e : 23 </li>
<li> b : 8 </li>
<li> c : 15 </li>
<li> a : 4 </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
但是通过变量名称via对dict的引用 …