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.
Tarkus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.