Django – NoReverseMatch at /carro/

I’m learning how to make a basic e-commerce site, but I have this problem that appears when I add a product in the cart. I tried everything (I think) but still can’t make this cart to work.
It seems that the argument for “id_producto” is not reaching to nowhere.

URLS.PY

from django.conf import settings
from django.urls import path
from .import views

app_name = "carro"

urlpatterns = [
    path('', views.detalle_carro, name='detalle_carro'),
    path('add/<str:id_producto>/', views.carro_mas, name='carro_mas'),
    path('remove/<str:id_producto>/', views.carro_menos, name='remover_carro'),
]

VIEWS.PY

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.views.decorators.http import require_POST
from gestionPedidos.models import Producto
from .carro import Carro
from .forms import anadirProductoForm
#aqui va el form cupon

@require_POST
def carro_mas(request, id_producto):
    carro = Carro(request)
    producto = get_object_or_404(Producto, id_producto=id_producto)
    form = anadirProductoForm(request.POST)
    
    if form.is_valid():
        cd = form.cleaned_data
        carro.anadir(producto = producto, cantidad=cd['cantidad'], update_cantidad=cd['update'])
    messages.success(request, "vamooo")
    return redirect('carro:detalle_carro')

def carro_menos(request, id_producto):
    carro = Carro(request)
    producto = get_object_or_404(Producto, id=id_producto)
    carro.remover(producto)
    return redirect('carro:detalle_carro')

def detalle_carro(request):
    carro = Carro(request)
    for item in carro:
        item['update_cantidad_form'] = anadirProductoForm(initial = {
            'cantidad' : item['cantidad'],
            'update' : True
        })
    return render(request, 'detalle_carro.html', {'carro' : carro})

my template

{% extends 'base.html' %}
{% load static %}

{% block title %}Mi carrito de compras!{% endblock %}

{% block first_content %}
    <div class = "row">
        <div class = "col-md-12">
            <h1>Mi carrito de compras</h1>
            
            <table class="table">
                <thead>
                    <th>Imagen</th>
                    <th>Nombre</th>
                    <th>Cantidad</th>
                    <th>Remover</th>
                    <th>Precio unitario</th>
                    <th>Precio total</th>
                </thead>
                <tbody>
                    {% for item in carro %}
                        {% with p=item.Producto %}
                            <tr>
                                <td>
                                    {% if p.imagen %}
                                    <img class="img-afiche"  src="{{p.imagen.url}}" class="card-img-top p-2" alt="{{p.nombre}}">
                                    {% else %}
                                    <img src="{% static 'img/image_not_found.jpg' %}" class="card-img-top" alt="not found">
                                    {% endif %}                        
                                </td>
                                <td>
                                    <h2>{{p.nombre}}</h2>
                                </td>
                                <td>
                                    <form action="{% url 'carro:carro_mas' p.id_producto %}" method="post">
                                        {{ item.update_cantidad_form.cantidad}}
                                        {{ item.update_cantidad_form.update}}
                                        <input class='btn btn-default' type="submit" value="Actualizar">
                                        {% csrf_token %}
                                    </form>
                                </td>
                                <td><a href="{% url 'carro:carro_menos' p.id_producto %}">Remover</a></td>
                                <td> {{item.precio}} </td>
                                <td> {{item.precio_total}} </td>
                            </tr>
                        {% endwith %}
                    {% endfor %}  
                </tbody>
            </table>
        </div>
        
        <div class="row">
            <div class="col-md-4" style="text-align : right">
                <a class="btn btn-default" href="{% url 'home' %}">Seguir comprando</a>
            </div>
        </div>
    </div>
{% endblock %}        

TRACEBACK

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/carro/

Django Version: 5.0.4
Python Version: 3.12.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core',
 'gestionPedidos',
 'carro']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template D:DUOC PORTAFOLIIOPP2024ProyectoPortafolio2024carrotemplatesdetalle_carro.html, error at line 35
   Reverse for 'carro_mas' with arguments '('',)' not found. 1 pattern(s) tried: ['carro/add/(?P<id_producto>[^/]+)/\Z']
   25 :                                     {% if p.imagen %}
   26 :                                     <img class="img-afiche"  src="{{p.imagen.url}}" class="card-img-top p-2" alt="{{p.nombre}}">
   27 :                                     {% else %}
   28 :                                     <img src="{% static 'img/image_not_found.jpg' %}" class="card-img-top" alt="not found">
   29 :                                     {% endif %}                        
   30 :                                 </td>
   31 :                                 <td>
   32 :                                     <h2>{{p.nombre}}</h2>
   33 :                                 </td>
   34 :                                 <td>
   35 :                                     <form action=" {% url 'carro:carro_mas' p.id_producto %} " method="post">
   36 :                                         {{ item.update_cantidad_form.cantidad}}
   37 :                                         {{ item.update_cantidad_form.update}}
   38 :                                         <input class='btn btn-default' type="submit" value="Actualizar">
   39 :                                         {% csrf_token %}
   40 :                                     </form>
   41 :                                 </td>
   42 :                                 <td><a href="{% url 'carro:carro_menos' p.id_producto %}">Remover</a></td>
   43 :                                 <td> {{item.precio}} </td>
   44 :                                 <td> {{item.precio_total}} </td>
   45 :                             </tr>


Traceback (most recent call last):
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangocorehandlersexception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangocorehandlersbase.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOPP2024ProyectoPortafolio2024carroviews.py", line 34, in detalle_carro
    return render(request, 'detalle_carro.html', {'carro' : carro})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangoshortcuts.py", line 25, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplateloader.py", line 62, in render_to_string
    return template.render(context, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebackendsdjango.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 171, in render
    return self._render(context)
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplateloader_tags.py", line 159, in render
    return compiled_parent._render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 163, in _render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplateloader_tags.py", line 65, in render
    result = block.nodelist.render(context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatedefaulttags.py", line 242, in render
    nodelist.append(node.render_annotated(context))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatedefaulttags.py", line 549, in render
    return self.nodelist.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 1000, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatebase.py", line 961, in render_annotated
    return self.render(context)
           ^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangotemplatedefaulttags.py", line 479, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangourlsbase.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:DUOC PORTAFOLIIOambambiente02Libsite-packagesdjangourlsresolvers.py", line 851, in _reverse_with_prefix
    raise NoReverseMatch(msg)
    ^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: NoReverseMatch at /carro/
Exception Value: Reverse for 'carro_mas' with arguments '('',)' not found. 1 pattern(s) tried: ['carro/add/(?P<id_producto>[^/]+)/\Z']

I expect the detalle_carro.html to render with the item product added.

New contributor

Tarkus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật