我试图在html模板中获得产品的购物车总额,而特定产品的总额工作正常,购物车总价/数量显示空格。
型号:
from django.db import models
import Accounts.models as accounts_models
import Products.models as products_models
class Order(models.Model):
customer = models.ForeignKey(accounts_models.Customer, on_delete=models.SET_NULL, blank=True, null=True)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.id)
@property
def get_cart_total(self):
orderproducts = self.orderproduct_set.all()
total = sum([product.get_total for product in orderproducts])
return total
@property
def get_cart_products(self):
orderproducts = self.orderproduct_set.all()
total = sum([product.quantity for product in orderproducts])
return total
class OrderProduct(models.Model):
product = models.ForeignKey(products_models.Products, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
@property
def get_total(self):
total = self.product.price * self.quantity
return total
视图:
def cart(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderproduct_set.all()
else:
items = []
context = {'items': items}
return render(request, 'cart.html', context)
html模板:
{% for item in items %}
<tr>
<td>{{ item.product.name }}</td>
<td>{{ item.quantity }}</td>
<td>${{ item.product.price }}</td>
<td>${{ item.get_total }}</td>
</tr>
{% endfor %}
<tr>
<td colspan="3"><strong>Total:</strong></td>
<td>${{ order.get_cart_total }}</td>
</tr>
我怎样才能开始在html模板中显示总数呢?
1条答案
按热度按时间laik7k3q1#
您没有在购物车视图中传递订单,以下是更新的视图: