python - 在django模板中相乘

标签 python django django-templates

我正在循环浏览购物车商品,并希望像这样将数量乘以单价:

{% for cart_item in cart.cartitem_set.all %}
{{cart_item.quantity}}*{{cart_item.unit_price}}
{% endfor %}

有没有可能做这样的事情?任何其他方式来做到这一点!谢谢

最佳答案

您可以使用 widthratio 内置过滤器进行乘法和除法。

计算 A*B: {% widthratio A 1 B %}

计算 A/B: {% widthratio A B 1 %}

来源:link

注意:对于无理数,结果会四舍五入为整数。

关于python - 在django模板中相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588160/

相关文章:

python - 在同一目录中导入文件

python - 与 Python 3 中的进程 ID 混淆

Apple Store Connect 的 Python JSON Web token (JWT)GET 请求 401 错误

python - 将十进制转换为十六进制python

python - 我遇到了来自 Django 的 404 错误消息。它说 "No location found matching the query"

python - 如何在 django 的 base.html 中检查用户是否已通过身份验证?

Djangocollectstatic无法使用django-storages(使用AWS)

django 将常用字典传递给模板而不在每个 View 中显式传递它们?

python - 如何动态地将 html 类添加到 Django 模板 'for-loop'?

Django 模板 : Dynamic template variable inside another variable