Python + Django(总价和物品不变!)

Python + Django (total price and items are not changing!)

我在 cart.html 中有这个:Cart

当我进行更改时,我将在下方显示最后带有绿色加号的总价已更改,但在我进行更改后总价和上涨的项目根本没有显示!

所以我这样做了:

在 class 我添加的 OrderItem 中:

@property
def get_total(self):
    total = self.product.price * self quantity
    return total

然后我转到我的模板 -----> cart.html 并将静态总价更改为 40 美元

对此:<div style="flex:1">${{item.get_total}}</div>

这些更改一切正常,但随后我进行了如下更改

在 class 我添加的订单:

@property
def get_cart_total(self):
    orderitems = self.orderitem_set.all()
    total = sum([item.get_total for item in orderitems])
    return total

@property
def get_cart_items(self):
    orderitems = self.orderitem_set.all()
    total = sum([item.quantity for item in orderitems])
    return total

然后我去 views.py 做这个改变:

来自上下文 = {'items':items} ----> context = {'items':items, 'order':order}

然后在 cart.html 中进行这些更改:

<th><h5>Items: <strong>3</strong></h5></th>  ------>  <th><h5>Items: <strong>{{order.get_cart_items}}</strong></h5></th>

<th><h5>Total: <strong></strong></h5></th> ----->  <th><h5>Total: <strong>${{order.get_cart_total}}</strong></h5></th>

因此,根据我对 class Order 所做的更改,运行服务器上没有任何更改!

views.py

models.py

以后请 post 您的代码 code blocks 而不是屏幕截图。

您遇到的问题是 cart 视图的上下文不包含 order 对象。

已解决!

这是因为 get 之前的命名空间!

谢谢大家!