为什么 Django 中的 Sum() 在计算只有 2 位小数的数字时返回小数点后 13 位的数字?



我正在学习Django并创建一个购物车应用程序。 我在这里要做的是在 HTML 页面上显示所有购物车项目"价格"的总和。

现在,我知道这可能是最糟糕的方法,因为我看过很多教程,但我宁愿一步一步地学习。

所以我在这里所做的是使用此处记录的Sum()函数将变量"总计"设置为等于所有价格的总和。

问题是它显示一个小数点后 13 位的数字,尽管我的模型只允许小数点后 2 位。如何克服此问题?

这是所有代码,显然,如果您有任何关于更好的"基本"方法(最佳实践(的提示,请在评论中告诉我。

"models.py">

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Item(models.Model):
name = models.CharField(max_length=25)
price = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return self.name

class Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
items = models.ManyToManyField(Item)
def __str__(self):
return 'Order number: %s' % self.id

"views.py">

from django.shortcuts import render
from .models import Cart, Item
from django.db.models import Sum

# Create your views here.
def home(request):
items = Item.objects.all()
carts = Cart.objects.all()
length = len(Cart.objects.all())
cart = carts[length - 1]
cart_items = cart.items.all()
total = cart_items.aggregate(Sum('price'))['price__sum']
return render(request, 'cart/home.html', {'cart': cart,
'items': items,
'cart_items': cart_items,
'total': total})

和我的"家.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home page</title>
</head>
<body>
<div><h1>Menu</h1></div>
{% for item in items %}
<ul>{{ item }} £{{ item.price}} <button>Add</button></ul>
{% endfor %}
<div><h1>Order</h1></div>
{{ cart }}
{% for item in cart_items %}
<ul>{{ item.name }} {{ item.price }}</ul>
{% endfor %}
<h2>Total</h2>
{{ total }}
</body>
</html>

如果您使用的是 SQLite,您应该知道它内部不支持十进制,如此处所述。这就是为什么Sum()返回一个float

因此,您应该使用 python 浮点数格式化函数将total舍入到 2 位小数,或者您可以使用floatformat模板过滤器。

您可以将总数设置为字符串,然后将其发送到上下文中的 HTML 页面。

total = cart_items.aggregate(Sum('price'))['price__sum']
total = '{:0.2f}'.format(total)

或者,如果您希望将其格式化为带有美元符号的字符串并添加逗号(数千美元(:

from django.contrib.humanize.templatetags.humanize import intcomma
...
total = cart_items.aggregate(Sum('price'))['price__sum']
total = f"${intcomma('{:0.2f}'.format(total))}"

最新更新