Saturday, August 13, 2022

Re: Help on my cart.js file using Django 3.2.15 in python

It seems that the error message you shared indicates the syntax error , tho.


On Aug 13, 2022, at 4:05 PM, Dieu merci Dramani <dieumercidramani@gmail.com> wrote:


This work :
productId:  1 action:  add
cart.js:9 USER: setraco
cart.js:20 setraco is logged in ! Sending data ...

cart.js:22        
Here is the errors that I'm trying without success:
  
updateUserOrder @ cart.js:22
(anonymous) @ cart.js:14

VM29:2 Uncaught (in promise) SyntaxError: Unexpected token '<', "
<!DOCTYPE "... is not valid JSON
Promise.then (async)
updateUserOrder @ cart.js:33
(anonymous) @ cart.js:14

Here my codes are :
cart.js side code:
var upadateButtons = document.getElementsByClassName('update-cart')


for( i = 0; i < upadateButtons.length; i++){
upadateButtons[i].addEventListener('click', function(){
var productId = this.dataset.product
var action = this.dataset.action
console.log('productId: ',productId, 'action: ',action)
console.log('USER:', user)

if (user == 'AnonymousUser'){
console.log(user, 'is not authenticated !')
}else{
updateUserOrder(productId, action)
}
})
}

function updateUserOrder(productId, action){
console.log(user, 'is logged in ! Sending data ...')
var url = "/update_Item/"
fetch(url,{
method:"post",
headers:{
"Content-Type":"application/json",
"X-CSRFToken" :"csrftoken",
},
body:JSON.stringify({url, 'productId:': productId, 'action: ':action})
})
.then((response) => {
return response.json()
})
.then((data) => {
console.log('data: ', data)
location.reloads()
})
}
html side update button :
<button data-product={{product.id}} data-action="add"
class="btn btn-outline-secondary add-btn update-cart">
Ajouter au Panier
</button>
views.py side :

def updateItems(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action: ', action)
print('Product Id: ', 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.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)

orderItem.save()

if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was updated', safe=False)
urls :
from django.urls import path
from . import views
from django.conf.urls.static import *


urlpatterns = [
#path('admin/', admin.site.urls),
path('', views.store, name="store"),
path('cart/', views.cart, name="cart"),
path('checkout/', views.checkout, name="checkout"),

path('update_Item/', views.updateItems, name="update_Item"),
]


Thanks for your help.

+243 82 83 010 21



--
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 django-users+unsubscribe@googlegroups.com.

No comments:

Post a Comment