gpt4 book ai didi

python - Django / python : Calculate total amount of item in django template

转载 作者:行者123 更新时间:2023-11-28 19:19:45 25 4
gpt4 key购买 nike

如何在 Django 模板中计算总数?

假设我想生成一份客户订单 list ,例如:

我有以下数据:

obj = [{"location": "name", "timeSlot": '09:30 PM', "price": 2}, {"location": "name", "timeSlot": '09:30 PM', "price": 1}, {"location": "name", "timeSlot": '09:30 PM', "price": 3}, {"location": "name", "timeSlot": '09:30 PM', "price": 2}, {"location": "name", "timeSlot": '09:30 PM', "price": 4}]

期望的报告输出

 --------------------
09:30 PM
--------------------
1 name $ 2
2 name $ 1
3 name $ 3
4 name $ 4
------------------------
Total $ 10

最佳答案

模板不是执行此操作的正确位置。您应该在您的 View 或模型中执行此操作。例如,在您收到订单后,您可以执行以下操作:

orders=order.objects.filter(id=timeslot)
total = 0
for order in orders:
total += order['price']

return render(request, 'your_template', {'orders': orders, 'total': total})

现在,总计将与订单一起出现在您的模板中

关于python - Django / python : Calculate total amount of item in django template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28276781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com