Saturday, August 13, 2022

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

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:
  
POST http://127.0.0.1:8000/update_Item/ 403 (Forbidden)
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADDd42bBDg78Pu7YbLpKRWrRjyuwkoY0NBnx3%3DX%3Dt-Tt5Od%2B7w%40mail.gmail.com.

No comments:

Post a Comment