我怎样才能用找不到'(,)'参数解决'Add'的反向。尝试了 1 种模式: ['agregar/(?P<producto_id>[0-9]+)/\\Z'] 错误?



我正在做一个django教程,当我试图添加一个href按钮时,我得到了这个错误,我使用django 4.0.4版本和python 3.9版本。

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"),
...

)

导致错误的模板目录。消息错误突出显示{% url 'Add' producto.id %}

{% for producto in response %}
<div class="col-6">
<div class="card" style="height: 10rem; width: 23rem; margin: 5px 0px;">
<div class="card-body">
<h5 class="card-tittle">{{producto.nombre}}</h5>
<p class="card-text">{{producto.marca}}</p>
<p class="card-text">{{producto.precio}}</p>
<a href="{% url 'Add' producto.id %}" class="btn btn-primary">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")

当我到达目录模板时,我得到了前面提到的错误,当我不使用{% url 'Add'产品时,api工作完美。Id %},我认为这是与Id有关的东西,可能看不懂??我不知道是不是这样。我不知道如何解决这个问题,也不知道哪里出了问题。

如何解决这个问题?

欢迎任何建议或帮助。

编辑:

来自cmd:

的完整错误信息
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersexception.py", line 55, in inner
response = get_response(request)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersbase.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersgiovaDesktopPagina-Web-Django-con-Apitiendaviews.py", line 17, in catalogo
return render(request, 'catalogo.html', {'response' : datos })
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangoshortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplateloader.py", line 62, in render_to_string
return template.render(context, request)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebackendsdjango.py", line 62, in render
return self.template.render(context)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 175, in render
return self._render(context)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 167, in _render
return self.nodelist.render(context)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 1000, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 958, in render_annotated
return self.render(context)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatedefaulttags.py", line 238, in render
nodelist.append(node.render_annotated(context))
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatebase.py", line 958, in render_annotated
return self.render(context)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangotemplatedefaulttags.py", line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsbase.py", line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:UsersgiovaAppDataLocalProgramsPythonPython310libsite-packagesdjangourlsresolvers.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']

好的,伙计们,我只是找到了我的问题的错误,我要在这里发布其他有同样问题的人。

在我的序列化器中我没有指定'id'

class ProductoSerializer(serializers.HyperlinkedModelSerializer) :
class Meta:
model = Producto
fields = ['url', 'nombre', 'codigo', 'precio', 'serie_producto', 'marca']

所以,解是:

class ProductoSerializer(serializers.HyperlinkedModelSerializer) :
class Meta:
model = Producto
fields = ['id' #here ,'url', 'nombre', 'codigo', 'precio', 'serie_producto', 'marca']
django.urls.exceptions.NoReverseMatch: Reverse for 'Add' with arguments '('',)' not found. 1 pattern(s) tried: ['agregar/(?P<producto_id>[0-9]+)/\Z']

这个错误告诉你,名称为'Add'的视图期望一个整数参数,但你传递给它一个空字符串''。为了调试它,您可以在render()调用之前添加print(datos)。您很可能会看到一个JSON对象列表,其中至少有一个具有id: ""

相关内容

  • 没有找到相关文章

最新更新