Django cart.get_total_price [<class 'decimal.ConversionSyntax'>]

I have a cart app in my Django Ecommerce web-site and when I click on update, when I need to update item quantity in the cart, I get an error [<class ‘decimal.ConversionSyntax’>], which highlights this snippet in onlineshop/base.html : {{ cart.get_total_price }}GEL base.html:

{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">

  <meta charset="utf-8">
  <title>{% block title %}My Shop{% endblock %}</title>
  <link href="{% static "css/base.css" %}" rel="stylesheet">

  <div id="header">
    <a href="/" class="logo">My Shop</a>
  <div id="subheader">
    <div class="cart">
      {% with total_items=cart|length %}
      {% if total_items > 0 %}
      Your cart:
      <a href="{% url "cart:cart_detail" %}">
        {{ total_items }} item{{ total_items|pluralize }},
        {{ cart.get_total_price }}GEL
      {% else %}
      Your cart is empty
      {% endif %}
      {% endwith %}

  <div id="content">
    {% block content %}

    {% endblock %}


from decimal import Decimal
from django.conf import settings
from onlineshop.models import Product

class Cart(object):

    def save(self):
        self.session.modified = True

    def __init__(self, request):
        self.session = request.session
        cart = self.session.get(settings.CART_SESSION_ID)
        if not cart:
            cart = self.session[settings.CART_SESSION_ID]={}
        self.cart = cart

    def add(self, product, quantity=1, override_quantity=False):
        product_id = str(
        if product_id not in self.cart:
            self.cart[product_id]={'quantity':0, 'price':str(product.price)}

        if override_quantity:
            self.cart[product_id]['quantity'] = quantity
            self.cart[product_id]['quantity'] += quantity

    def remove(self, product):
        product_id = str(
        if product_id in self.cart:
            del self.cart[product_id]

    def __iter__(self):
        product_ids = self.cart.keys()
        products = Product.objects.filter(id__in=product_ids)
        cart = self.cart.copy()
        for product in products:
            cart[str(]['product'] = product

        for item in cart.values():
            item['price'] = Decimal(item['price'])
            item['total_price'] = item['price'] * item['quantity']
            yield item

    def __len__(self):
        return sum(item['quantity'] for item in self.cart.values())

    def get_total_price(self):
        return sum(Decimal(item['price'] * item['quantity']) for item in self.cart.values())

    def clear(self):
        del self.session[settings.CART_SESSION_ID]

Please post the complete stack trace you’re receiving. Enclose it between lines of three backticks - ` to ensure it stays properly formatted.

InvalidOperation at /

[<class 'decimal.ConversionSyntax'>]

Request Method: 	GET
Request URL:
Django Version: 	3.2.3
Exception Type: 	InvalidOperation
Exception Value: 	

[<class 'decimal.ConversionSyntax'>]

Exception Location: 	C:\Users\l\desktop\Project\netsetshop\cart\, line 54, in <genexpr>
Python Executable: 	C:\Users\l\desktop\Project\netshop\Scripts\python.exe
Python Version: 	3.9.0
Python Path: 	


Server time: 	Sat, 22 May 2021 14:43:12 +0000

Line 54 is this:

The only place where I see the ‘price’ key being set is here:

You’re storing price as a string, not as a decimal. A string multiplied by an integer (quantity) gives you “quantity” repetitions of the string.
“1.50” * 4 = “1.501.501.501.50”, which is an improperly formatted decimal value.

Side note: When you’re asked for a trace back, it’s more helpful to post the code trace back in your console window where you’re running runserver (or runserver_plus), not the error message you’re receiving in your browser.

turns out the problem was that I had whole sum in get_total_price in Decimal field and that was causing the error. thanks anyways