Reverse for 'add' with keyword Arguments '{'product_id': 2}' not found. 1 Pattern(s) tried: ['Cart/add/<int:product_id>/']

Asked

Viewed 22 times

-2

Hello I’m studying Django and following a tutorial I got to the part of adding a product to my shopping bag, but when I click the add button in the bag it returns this error: Imagem do erro na pagina web

Traceback takes me to this part of the code:

Imagem do traceback

My.py urls code is like this:

from django.urls import path
from .views import cart_detail, cart_add, cart_remove

app_name = "cart"

urlpatterns = [
    path("", cart_detail, name="detail"),
    path("add/<int:product_id>/", cart_add, name="add"),
    path("remove/<int:product_id>/", cart_remove, name="remove"),
]

My cart_add that redirects me to the cart/bag details:

@require_POST
def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)

    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(
            product=product, quantity=cd['quantity'], override_quantity=cd['override'],
        )

    return redirect('cart:detail')

And my cartoon looks like this:

def cart_detail(request):
    cart = Cart(request)
    return render(request, 'cart/sacola.html', {'cart': cart})

Add to cart/bag action is as follows in my html:

<form class="form-group" action="{% url 'cart:add' product.id %}" method="POST">
    <p class="form-inline">
       {{ form.quantity.label_tag }}
       {% render_field form.quantity class+="form-control ml-sm-3" %}
       {{ form.override }}
    </p>
    {% csrf_token %}
    <input class="btn btn-success" type="submit" value="Adicionar à Sacola">
</form> 

1 answer

0

The error was that I made the addition in the cart by Siession and then I took the products from my site, but in Siession the product was recorded and ended up giving error when trying to access the cart. Workaround: Delete Session and then start a new.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.