我有一个按类型过滤的查询“fruits”
fruits = {apple,orange,mangoes,pineapples等}
然而,在这种情况下,每种类型的水果也有不同的物品。 (对于开发人员:服务器中域的属性)http://i46.tinypic.com/35kj1o7.png
请注意: - 水果的种类可能会有所不同,有些水果不适合季节。 - 对于开发人员:单个服务器可能拥有不同数量的域 - numType字段,用于动态确定当天可用的“水果”数量
主要目标: - 在表格中显示和排序可用数据。
http://i48.tinypic.com/infhip.png
在view.py上,我做了2个原始sql查询
提前感谢!
答案 0 :(得分:2)
我不确定是否完全按照这个问题进行操作,但请尝试查看Django template for loops上的文档,您可以使用该标记循环键/值对。
如果它比那更复杂,你能尝试多解释一下吗?
也许regroup会做你想要的事情?
{% regroup fruit_list by [key] as apple_list %}
{% for apples in apple_list %}
<table>
{% for apple in apples %}
<td>{{apple}}</td>
{% endfor %}
</table>
{% endfor %}
答案 1 :(得分:0)
我可能错了,可能有更好的方法,欢迎大家分享和讨论。
目前的缺陷:dicts是无序的
在我的view.py上:
我做了两个查询,
//这将告诉我当天出售的水果类别
//将查询集django转换为列表 fruitRows = list(fruit_list)
lenRow = len(fruitRows)
//在python中创建一个dict masterFruitList = {};
//根据数量在dict中创建密钥 对于范围内的i(int(number_of_cat)): masterFruitList ['fruit_Type'+ str(i + 1)] = []
//向dict添加行
//添加到上下文 return render_to_response('data.html',{'numdo':masterFruitList})
template.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 %}