Myrodis
Myrodis

Reputation: 75

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">

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

<body>
  <div id="header">
    <a href="/" class="logo">My Shop</a>
  </div>
  <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
      </a>
      {% else %}
      Your cart is empty
      {% endif %}
      {% endwith %}

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

    {% endblock %}
  </div>
</body>

</html>

cart.py:

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(product.id)
        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
        else:
            self.cart[product_id]['quantity'] += quantity
        self.save()


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

    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.id)]['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]
        self.save()

cart/views.py:

from django.shortcuts import render, get_object_or_404, redirect
from onlineshop.models import *
from .cart import Cart
from django.views.decorators.http import require_POST
from .forms import *


@require_POST
def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(product=product, quantity=cd['quantity'], override_quantity=cd['override'])
    return redirect ('cart:cart_detail')


def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product)
    cart.remove(product)
    return redirect ('cart:cart_detail')


def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(initial={'quantity':item['quantity'], 'override':True})
    context = {
        'cart':cart,
    }
    return render(request, 'cart/detail.html', context)

Upvotes: 0

Views: 163

Answers (1)

Myrodis
Myrodis

Reputation: 75

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

Upvotes: 0

Related Questions