*cart.py*

class Cart(object):
def __init__(self,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,update_quantity = False):
'''
Add a product to the cart or update its quantity

'''
product_id = str(product.id)
print(product_id)
if product_id not in self.cart:
self.cart[product_id] = {'quantity':0,'price':str(product.price)}

if update_quantity:
self.cart[product_id]['quantity'] = quantity


else:
self.cart[product_id]['quantity']+=quantity

self.save()

def save(self):
#mark the session as "modidied" to make sure it gets 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 __iter__(self):
'''
Iterate over the items in the cart and get the products from the database

'''
product_ids = self.cart.keys()
# get the product objects and add them to the cart
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):
"""
count all the items in the cart
"""
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):
#remove cart from session
del self.session[settings.CART_SESSION_ID]
self.save()


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


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



def cart_detail(request):
cart = Cart(request)
for item in cart:
* print(item)*
for item in cart:
item['update_quantity_form'] = forms.CartAddProductForm(
                                               initial =
{'quantity':item['quantity'],
                                              'update' : True}

                                              )

return render (request,'cart/cartdetail.html',{'cart':cart})

here len of item is 0



On Wed, May 22, 2019 at 6:58 PM Anirudh Jain <[email protected]>
wrote:

> Can you tell which function in which app you are using to show data in
> which template ?
>
> On Wed, 22 May 2019, 18:56 Soumen Khatua, <[email protected]>
> wrote:
>
>> Hi Folks,
>> Guys I'm not getting my data into html after rendered that still I'm not
>> getting my data,i'm sharing my github link:
>> https://github.com/Herosoumen/clothproject.Please go through this
>> link.Please guys help me it's urgent.
>>
>>
>> Thank You
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAPUw6WZ-ESxGfTZwiSBUf7ZSiDF3g0jzSNqUX3N4NowKA3%3DGTg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAPUw6WZ-ESxGfTZwiSBUf7ZSiDF3g0jzSNqUX3N4NowKA3%3DGTg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAC3mK7dv0yZwX6-RjfRwkpTAF0j5%2Bxq-L1O8NcCFLj1pY-uQjQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAC3mK7dv0yZwX6-RjfRwkpTAF0j5%2Bxq-L1O8NcCFLj1pY-uQjQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPUw6Wa%3Do0tUVFj4e2wj9g%2BnZk0w1BFOqWdeqg%3DxZZx6WSDD%3DA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to