-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:
Traceback takes me to this part of the code:
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>