我在库存模型中有项目,每个项目都有所属的类别。我希望它循环访问库存模型,查找不同的类别,显示与该类别相关的项目,并将类别名称作为标题显示在表中相关项目的上方。
model.py
class Stock(models.Model):
user = models.ForeignKey(User, on_delete = models.SET_NULL, null = True)
part_No = models.CharField(max_length=100, null=True)
item_name = models.CharField(max_length=100, null=True)
category = models.CharField(max_length=100, null=True)
unit = models.CharField(max_length=50, null=True)
balance_bd = models.IntegerField(default='0', null = True)
received = models.IntegerField(default='0', null = True)
issued = models.IntegerField(default='0', null=True)
unit_price = models.DecimalField(max_digits=20, decimal_places=2, null=True)
obsolete = models.BooleanField(default=False, null=True)
views.py
def stock(request):
stocks = Stock.objects.all()
context = {
'stocks':stocks,
}
return render(request, 'base/stock.html', context)
html模板
<!-- DATA TABLE-->
<div class="table-responsive m-b-40">
<table class="table table-borderless table-data3">
<thead>
<tr class="bg-primary">
<th>NO</th>
<th>PART NO</th>
<th>DESCRIPTION</th>
<th>CATEGORY</th>
<th>UNIT</th>
<th>BALANCE B/D</th>
<th>RECEIVED</th>
<th>TOTAL BAL</th>
<th>ISSUED</th>
<th>TALLY CARD BAL</th>
<th>UNIT PRICE</th>
<th>TOTAL PRICE</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
{% for stock in stocks %}
{% if stock.obsolete %}
<tr>
<td>{{forloop.counter}}</td>
<td class="text-success"><i>{{stock.part_No}}</i></td>
<td class="text-success"><i>{{stock.item_name}}(obsolete)</i></td>
<td class="text-success"><i>{{stock.category}}</i></td>
<td class="text-success"><i>{{stock.unit}}</i></td>
<td class="text-success"><i>{{stock.balance_bd}}</i></td>
<td class="text-success"><i>{{stock.received}}</i></td>
<td class="text-success"><i>{{stock.total_bal}}</i></td>
<td class="text-success"><i>{{stock.issued}}</i></td>
<td class="text-success"><i>{{stock.tally_card_bal}}</i></td>
<td class="text-success"><i>{{stock.unit_price}}</i></td>
<td class="text-success"><i>¢{{stock.total_price}}</i></td>
<td>
<div class="table-data-feature">
<a href="{% url 'update-item' stock.id %}"> <button class="item" data-toggle="tooltip" data-placement="top" title="Update">
<i class="zmdi zmdi-edit"></i>
</button></a>
<button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="zmdi zmdi-delete"></i>
</button>
</div>
</td>
</tr>
{% else %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stock.part_No}}</td>
<td>{{stock.item_name}}</td>
<td>{{stock.category}}</td>
<td>{{stock.unit}}</td>
<td>{{stock.balance_bd}}</td>
<td>{{stock.received}}</td>
<td>{{stock.total_bal}}</td>
<td>{{stock.issued}}</td>
<td>{{stock.tally_card_bal}}</td>
<td>¢{{stock.unit_price}}</td>
<td>¢{{stock.total_price}}</td>
<td>
<div class="table-data-feature">
<a href="{% url 'update-item' stock.id %}"> <button class="item" data-toggle="tooltip" data-placement="top" title="update">
<i class="zmdi zmdi-edit"></i>
</button></a>
<button class="item" data-toggle="tooltip" data-placement="top" title="Delete">
<i class="zmdi zmdi-delete"></i>
</button>
</div>
</td>
</tr>
{% endif %}
{% empty %}
<p>No item in stock</p>
{% endfor %}
<tr>
<td></td>
<td></td>
<td></td>
<td colspan="8" style="font-weight: bold;">TOTAL PRICE</td>
<td class="text-success" style="font-weight: bold;">¢{{sumTotalPrice}}</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<!-- END DATA TABLE-->
这是我目前得到的结果my stock table
我希望它看起来像image
1条答案
按热度按时间a11xaf1n1#
Django提供了regoup模板标签,你可以从Django官方文档中很容易地理解
链接在这里