我想要对密钥名称进行硬编码(即在我的模板中低于 msgid
,因为dict是动态的,所以我只想循环它 .
views.py
class Pymat_program(View):
def get(self, request, *args, **kwargs):
selected_xml = 'a_directory_to_my_file'
smsreport_dict = self.parse_xml_file(selected_xml)
html = self.populate_html_text(smsreport_dict)
return HttpResponse(html)
def populate_html_text(self, smsreport_dict):
t = get_template('template2.html')
html = t.render(smsreport_dict)
return html
template2.html
<p>MSGID: {{ msgid }}</p>
<p> Begin
{% for key, value in smsreport_dict %}
<tr>
<td> Key: {{ key }} </td>
</tr>
{% endfor %}
</p>End
在template2.html中,您可以看到被调用的msgid值(smsreport_dict中的几个值之一),该值显示在我的页面上 . 但由于某种原因,smsreport_dict循环不会产生任何文本 . 我哪里错了?
2 回答
smsreport_dict
应该在您使用的Context内部以呈现模板:另外,在遍历模板中的dict时忘记调用
.items
:您需要将
.items
添加到smsreport_dict
有关类似情况,请参阅here .