ecommerce cart error, shows path not found

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:
Screenshot 2024-01-14 at 2.27.55 PM

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

Please post your cart_add view.

i have updated the post with cart_add view.

It’s most likely that you’re generating the 404 on this line:

You need to validate that you’re sending the right data and that you have an instance of Products with that id.

(I am curious why you’re checking an action field for post rather than checking the request.method, but that’s not an issue here - just a question.)

i could use the print function to check if im getting the correct data right?

and action post is from ajax. i’m not good at ajax i just assumed this is how it works.

Yes, you can add a print to the view to see what you’re getting from the view. You can also look at the network tab in your browser’s developer tools to see what the browser is sending.


From Django’s perspective, it doesn’t matter whether a request is coming directly from the browser or from an AJAX call. In a typical Django view, you test request.method == 'POST' to determine whether the request is a GET or POST. See the example view at Working with forms | Django documentation | Django

With what you’ve posted, there’s no benefit to having action: 'post' in the data section of your request when you have the type: 'POST' available in that same request. (Now, my answer would be different if you’re doing something else with that value, but if you are, you’re not showing that here.)

i tried to debug with print and my product_id is returning none. i did not have a id field before ( i thought django will automatically create an id field), now that i have created an id with AutoField and did migrate.
But now its giving me a server 500 error with a value error in the console.

ValueError: Field 'id' expected a number but got ''.

probably since its none. but can you identify why its returning none?

I don’t know. You’re going to need to determine what your browser is sending.

1 Like