Home > Software design >  Django cart management
Django cart management

Time:09-22

I work on a cart management application, I use the django-shopping-cart 0.1 package, At the level of my project I use a dictionnary the products then add them to the basket, Also noted the products are added by setting the product code.

def cart_add(request, code):
   dico={"produits":[{'code':'fg21','name':'coca cola','prix':1500}, 
   {'code':'br21','name':'pomme','prix':1800}]}
   all_produits=dico['produits']
   all_product = next((item for item in all_produits if item["code"] == code), None)
   if selected_product != None:
     cart = Cart(request)
     product = selected_product 
    cart.add(product=product)

return render(request, 'cart/detail.html',context)

Here the error comes from the cart.py module of django-shopping-cart 0.1 At line level (id = product.id) I am told the dic object has no code attribute even if I do (id=product.code)

cart.py

class Cart(object):

def __init__(self, request):
    self.request = request
    self.session = request.session
    cart = self.session.get(settings.CART_SESSION_ID)
    if not cart:
        # save an empty cart in the session
        cart = self.session[settings.CART_SESSION_ID] = {}
    self.cart = cart

def add(self, product, quantity=1, action=None):
    """
    Add a product to the cart or update its quantity.
    """
    id = product.id
    newItem = True
    if str(product.id) not in self.cart.keys():

        self.cart[product.id] = {
            'userid': self.request.user.id,
            'product_id': id,
            'name': product.name,
            'quantity': 1,
            'price': str(product.price),
            'image': product.image.url
        }
    else:
        newItem = True

        for key, value in self.cart.items():
            if key == str(product.id):

                value['quantity'] = value['quantity']   1
                newItem = False
                self.save()
                break
        if newItem == True:

            self.cart[product.id] = {
                'userid': self.request,
                'product_id': product.id,
                'name': product.name,
                'quantity': 1,
                'price': str(product.price),
                'image': product.image.url
            }

    self.save()

def save(self):
    # update the session cart
    self.session[settings.CART_SESSION_ID] = self.cart
    # mark the session as "modified" to make sure it is saved
    self.session.modified = True

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

def decrement(self, product):
    for key, value in self.cart.items():
        if key == str(product.id):

            value['quantity'] = value['quantity'] - 1
            if(value['quantity'] < 1):
                return redirect('cart:cart_detail')
            self.save()
            break
        else:
            print("Something Wrong")

  def clear(self):
    # empty cart
    self.session[settings.CART_SESSION_ID] = {}
    self.session.modified = True

CodePudding user response:

It seems your variable product is a dictionary. In this case, you can only do product['id']. The syntax product.id would require product to be an object, not a dictionary.

  • Related