the selected items are not saved in database

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?

If you are getting an error with a traceback, please post the entire traceback.

this is the error but when i selected the color and size it dose not save in the database or admin side

Exception happened during processing of request from ('127.0.0.1', 51539)
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

This error appears to be outside Django itself.

Are you saying you’re getting this error every time?

If so, I’m inclined to believe there’s something not right with your installation.

What version of Python and Django are you using? Is this a development environment using runserver (or runserver_plus)? If not, how is this being run?

Do you have this running within a virtual environment?

Has this ever worked?

  • Do you have any other Django projects working in this same environment?

i was forget to activate the environment in my project that was the problem but even if i activate my environment i got the same error after some minutes i don’t know why it not working properly?