Search code examples
pythondjangoe-commercecart

cart total in Django shows blank space


I'm trying to get cart total sum of products in the html template, while the the total sum of particular products works fine, the cart total price/quantity shows blank spaces.

Models:

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

views:

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 template:

                {% 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>

How can I start showing the total numbers in the html template?


Solution

  • You didn't pass order in your cart view, here is updated view:

    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 = []
            order = None  # Add this line to handle the case when there is no order
        context = {'items': items, 'order': order}  # Add the order object to the context dictionary
        return render(request, 'cart.html', context)