I'm doing a tutorial from django, and i getting this error when trying to add an href button, i use django 4.0.4 version and python 3.9 version.
Models.py
class Producto(models.Model):
serie_producto = models.CharField(max_length=30)
nombre = models.CharField(max_length=30)
codigo = models.CharField(max_length=15)
marca = models.CharField(max_length=30)
precio = models.IntegerField()
created_date = models.DateTimeField(default=timezone.now)
urls.py
...
urlpatterns = [
...
path('agregar/<int:producto_id>/', agregar_producto, name="Add"),
...
]
template catalogo that causes te error. The message error highlights {% url 'Add' producto.id %}
{% for producto in response %}
<div >
<div style="height: 10rem; width: 23rem; margin: 5px 0px;">
<div >
<h5 >{{producto.nombre}}</h5>
<p >{{producto.marca}}</p>
<p >{{producto.precio}}</p>
<a href="{% url 'Add' producto.id %}" >Agregar al carrito</a>
</div>
</div>
</div>
{% endfor %}
view.py
from tienda.models import Producto
from tienda.carrito import Carrito
def catalogo(request):
url = "http://127.0.0.1:8000/api/Producto/"
response = requests.get(url, auth=('admin','duoc'))
datos = response.json()
return render(request, 'catalogo.html', {'response' : datos })
def agregar_producto(request, producto_id):
carrito = Carrito(request)
producto = Producto.objects.get(id=producto_id)
carrito.agregar(producto)
return redirect("Tienda")
The Problem When i got to the catalogo template i get the before mentioned error, the api works perfectly when i dont use the {% url 'Add' producto.id %}, i think that is something with the id, cant read it maybe??, i dont know if is this. I have no idea how to solve this or what is wrong.
how can i solve this ?
any suggestion or help are welcome.
EDIT:
Complete error message from cmd:
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\giova\Desktop\Pagina-Web-Django-con-Api\tienda\views.py", line 17, in catalogo
return render(request, 'catalogo.html', {'response' : datos })
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 62, in render
return self.template.render(context)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 238, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 958, in render_annotated
return self.render(context)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\giova\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 802, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'Add' with arguments '('',)' not found. 1 pattern(s) tried: ['agregar/(?P<producto_id>[0-9] )/\\Z']
CodePudding user response:
ok guys i just fin the error to my problem, i going to post here for other people with the same problem.
In my serializer i dont specified the 'id'
class ProductoSerializer(serializers.HyperlinkedModelSerializer) :
class Meta:
model = Producto
fields = ['url', 'nombre', 'codigo', 'precio', 'serie_producto', 'marca']
soo, the solution is:
class ProductoSerializer(serializers.HyperlinkedModelSerializer) :
class Meta:
model = Producto
fields = ['id' #here ,'url', 'nombre', 'codigo', 'precio', 'serie_producto', 'marca']
CodePudding user response:
django.urls.exceptions.NoReverseMatch: Reverse for 'Add' with arguments '('',)' not found. 1 pattern(s) tried: ['agregar/(?P<producto_id>[0-9] )/\\Z']
This error tells you that the view with name 'Add'
expects an integer argument, but you passed it an empty string ''
. To debug this, you can add print(datos)
before your render()
call. Most likely you will see a list of JSON objects with at least one that has id: ""
.