gpt4 book ai didi

python - Django-不支持的操作数类型/: 'dict' and 'dict'

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:58 30 4
gpt4 key购买 nike

我不知道如何在 html 中显示“净利润”的结果,这是 net_profit/sales 的结果。我不想把这个除法公式直接放在html中,因为除此之外还有很多其他复杂的计算。

那么如何把这个除法结果同时放到views.py和html中呢?

以下代码返回错误作为标题。

views.py

def get_context_data(self, **kwargs):
context = super(XXXView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
context["net_profit"] = self.get_queryset().aggregate(Sum('net_profit'))

context["net_margin"] = context["net_profit"]/context["sales"] ---if here correct--?

HTML

to show result for sales: {{sales.sales__sum}}
to show result for net_profit: {{net_profit.net_profit__sum}}
how to show result for "net margin"?

最佳答案

如果像这样:

sales = context['sales']
net_profit = context['net_profit']

销售额和净利润是对象。因此,html 显示净利润为 {{ sales.sale__sum/net_profit.net_profit__sum }}

django模板可以使用数字的cal。

关于python - Django-不支持的操作数类型/: 'dict' and 'dict' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337414/

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