我可能错了,可能有更好的方法,欢迎大家分享和讨论。
当前缺陷:dicts是无序的
在我看来.py:
我做了两个查询,
- fruit_list = select * from main where type ="fruit"
// 这将告诉我当天出售的可用水果类别
- number_of_cat = 从 main 中选择价格,其中 type ="fruit" 和 name="numType"
// 将 django 返回的查询集转换成一个列表 fruitRows = list(fruit_list)
lenRow = len(fruitRows)
// 在 python masterFruitList = {} 中创建一个字典;
// 根据 for i in range(int(number_of_cat)) 的数量在 dict 中创建键:masterFruitList['fruit_Type'+ str(i+1)] = []
// 向字典中添加行
// 添加到上下文 return render_to_response('data.html', {'numdo' : masterFruitList })
模板.html:
关键是使用 .items 语法来迭代python字典
{% if numdo %}
{% for key,value in numdo.items %}
<p> hey! <b> {{ key }} </b>
<table class = "tablebord">
<tr>
<th> name </th>
<th> type </th>
<th> price </th>
</tr>
{% for x in value %}
<td class = "tablebord"> {{ x.name }} </td>
<td class = "tablebord"> {{ x.type }} </td>
<td class = "tablebord"> {{ x.price }} </td>
</tr>
{% endfor %}
</table>
</p>
{% endfor %}
{% endif %}