i have add to cart view like this
def add_cart(request,product_id):
product = Product.objects.get(id=product_id)
product_varation=[]
if request.method == 'POST':
for item in request.POST:
key = item
value = request.POST[key]
try:
varation = Variation.objects.get(product=product,varation_catagory__iexact = key,varation_value__iexact = value)
product_varation.append(varation)
except:
pass
try:
cart = Cart.objects.get(cart_id = _cart_id(request))
except Cart.DoesNotExist:
cart = Cart.objects.create(
cart_id = _cart_id(request)
)
cart.save()
try:
cart_item = CartItem.objects.get(product=product,cart=cart)
if len(product_varation) > 0:
cart_item.varations.clear()
for item in product_varation:
cart_item.varations.add(item)
cart_item.quantity +=1
cart_item.save()
except CartItem.DoesNotExist:
cart_item = CartItem.objects.create(
product=product,
quantity=1,
cart=cart,
)
if len(product_varation) > 0:
cart_item.varations.clear()
for item in product_varation:
cart_item.varations.add(item)
cart_item.save()
return redirect('cart')
and models.py
class Cart(models.Model):
cart_id = models.CharField(max_length=250,blank=True)
date_added = models.DateField(auto_now_add=True)
def __str__(self):
return self.cart_id
class CartItem(models.Model):
product = models.ForeignKey(Product,on_delete=models.CASCADE)
varations = models.ManyToManyField(Variation,blank=True)
cart = models.ForeignKey(Cart,on_delete=models.CASCADE)
quantity = models.IntegerField()
is_active = models.BooleanField(default=True)
def subtotal(self):
return self.product.price * self.quantity
def __unicode__(self):
return self.product
and my template look like this
<div class="row">
<div class="item-option-select">
<h6>Choose Color</h6>
<select name="Color" class="form-control" required>
<option value="" selected disabled>Select</option>
{% for i in single_product.variation_set.colors%}
<option value="{{i.varation_value}}">{{i.varation_value}}</option>
{%endfor%}
</select>
</div>
</div> <!-- row.// -->
<div class="row">
<div class="item-option-select">
<h6>Choose Size</h6>
<select name="Color" class="form-control" required>
<option value="" selected disabled>Select</option>
{% for i in single_product.variation_set.sizes%}
<option value="{{i.varation_value|lower}}">{{i.varation_value|capfirst}}</option>
{%endfor%}
</select>
</div>
</div>
so in the cart_item table the varation feilds is not selected as it is selected in the request …and also their is error in the command line “Exception happened during processing of request from (‘127.0.0.1’, 55572)”? so how can i fix this any one who can help me?