i have setup a cart for an ecommerce project but its throwing a 404 error with path not found.
cart urls.py
from django.urls import path
from .views import cart_view, cart_add, cart_update, cart_delete
urlpatterns = [
path('', cart_view, name='cart'),
path('add/', cart_add, name='cartAdd'),
path('update/', cart_update, name='cartUpdate'),
path('delete/', cart_delete, name='cartDelete'),
]
main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
path('about/', about_view, name='about'),
path('shop/', shop_view, name='shop'),
#path('single_product/', detail_view, name='productDetails'),
path('cart/', include('cart.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
below is the error i get:
below is how i called the path, in ajax:
$.ajax({
type: 'POST',
url: "{% url 'cartAdd' %}",
data: {
product_is: $('#addCart').val(),
csrfmiddlewaretoken: '{{ csrf_token }}',
action: 'post'
},
i have added the script in my base .html file so that it it available everywhere where i have to add to cart.
cart_add view function
def cart_add(request):
# get the cart instance
cart = Cart(request)
#test for method
if request.POST.get('action') == 'post':
product_id = request.POST.get('product_id')
# get the product from DB
product = get_object_or_404(Products, id=product_id)
#save to session
cart.add(product=product)
# return response
response = JsonResponse({'product: ': product.title})
return response