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

276 views
Skip to first unread message

Dieu merci Dramani

unread,
Aug 13, 2022, 3:05:53 AM8/13/22
to django...@googlegroups.com
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


tomo yamano

unread,
Aug 13, 2022, 2:57:54 PM8/13/22
to django...@googlegroups.com, django...@googlegroups.com
It seems that the error message you shared indicates the syntax error , tho.


--
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...@googlegroups.com.

Kasper Laudrup

unread,
Aug 13, 2022, 4:03:45 PM8/13/22
to django...@googlegroups.com
On 13/08/2022 09.04, Dieu merci Dramani 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:
> POST http://127.0.0.1:8000/update_Item/
> <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
>

Most likely your updateItems functions raises and exception causing an
HTTP 403 response. The error page is then being rendered as HTML by
Django which causes your Javascript code to fail when trying to parse it
as JSON.

You should most likely be able to find the root cause of the exception
in your log files or the output of your developer console so you known
how to fix it. I would guess something related to access rights on your
database or similar but the log should provide that information.

You should also consider a way of returning JSON instead of HTML when
returning errors to the client based on the clients "Accept" header. I'm
sure there's some generic way to do that but I haven't looked into it.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Kasper Laudrup

unread,
Aug 13, 2022, 4:53:14 PM8/13/22
to django...@googlegroups.com
On 13/08/2022 09.04, Dieu merci Dramani 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:
> POST http://127.0.0.1:8000/update_Item/
> <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
>

Kasper Laudrup

unread,
Aug 14, 2022, 5:15:32 AM8/14/22
to django...@googlegroups.com
On 13/08/2022 09.04, Dieu merci Dramani 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:
> POST http://127.0.0.1:8000/update_Item/ <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
>

Kasper Laudrup

unread,
Aug 14, 2022, 10:47:29 AM8/14/22
to django...@googlegroups.com
On 13/08/2022 09.04, Dieu merci Dramani 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:
> POST http://127.0.0.1:8000/update_Item/ <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
>

Reply all
Reply to author
Forward
0 new messages