POST http://127.0.0.1:9099/update_item/ 500 (Internal Server Error)

views.py

def updateItem(request):
	
	data = json.loads(request.body)
	productId = data['productId']
	action = data['action']

	print('action:', action)
	print('productId:', productId)

	customer = request.user.customer
	product = Product.objects.get(id=productId)
	order, created = Order.objects.get_or_create(customer=customer, complete=False)

	orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)
	if action == 'add':
		orderItem.quantity = (orderitem_set.quantity + 1)
	elif action == 'remove':
		orderItem.quantity = (orderItem.quantity - 1)

	orderItem.save()
	
	if orderItem.quantity <= 0:
		orderItem.delete()

	return	JsonResponse('Item was added', safe=False)	

cart.js

function updateUserOrder(productId, action){
	console.log('User is logged in, sending data...')

	var url = 'http://127.0.0.1:9099/update_item/'

	fetch(url, {
		method:'GET',
		headers:{
			'Content-Type':'application/json',
			'X-CSRFToken':csrftoken,
		},
		body:JSON.stringify({'productId': productId, 'action': action})
	})

	.then((response) =>{
		return response.json()
	})
	.then((data) =>{
		console.log('data:', data)
})

}	

Please post the complete traceback from the console where you’re running runserver. If this is a production environment, please post that traceback from the logs.

from the browser console…
According to my expectation, it should change the quantity in the cart.html

productId: 3 action: add
cart.js:9 USER: Excellent
cart.js:21 User is logged in, sending data...
cart.js:25 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
    at updateUserOrder (cart.js:25:2)
    at HTMLAnchorElement.<anonymous> (cart.js:13:4)
cart.js:7 productId: 3 action: add
cart.js:9 USER: Excellent
cart.js:21 User is logged in, sending data...
cart.js:25 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
    at updateUserOrder (cart.js:25:2)
    at HTMLAnchorElement.<anonymous> (cart.js:13:4)

Traceback

action: add
productId: 2
Internal Server Error: /update_item/
Traceback (most recent call last):
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\SERVO DIRECT 10(WRH)\Documents\NewFiles\foxicsite\foxic\myshop\views.py", line 53, in updateItem
    orderItem.quantity = (orderitem.quantity + 1)
NameError: name 'orderitem' is not defined
[29/Apr/2022 17:28:57] "POST /update_item/ HTTP/1.1" 500 67656

So that’s the line with the error, and the error being thrown.

What is “orderitem” supposed to be at that line in your code?

I’ve corrected it with orderItem but still showing error

Traceback (most recent call last):
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\SERVO DIRECT 10(WRH)\Documents\NewFiles\foxicsite\foxic\myshop\views.py", line 51, in updateItem
    orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\query.py", line 664, in get_or_create
    return self.create(**params), True
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\query.py", line 514, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\base.py", line 806, in save
    self.save_base(
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\base.py", line 857, in save_base
    updated = self._save_table(
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\base.py", line 1000, in _save_table
    results = self._do_insert(
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\base.py", line 1041, in _do_insert
    return manager._insert(
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\query.py", line 1434, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\models\sql\compiler.py", line 1621, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\utils.py", line 103, in execute
    return super().execute(sql, params)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\SERVO DIRECT 10(WRH)\AppData\Roaming\Python\Python310\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: myshop_orderitem.shipping_cost
[29/Apr/2022 17:52:40] "POST /update_item/ HTTP/1.1" 500 168166

So what do you think this error is telling you?

(And, looking at the full traceback, can you identify which line in your code is causing this error?)