python - Django 的 NoReverseMatch

标签 python django

我不知道为什么以及会出现什么问题,他们可以帮助我吗?预先感谢,问候!

Reverse for 'entregado' with arguments '()' and keyword arguments '{'cod_experto': 'ASE-0048', 'id_pedido': 1770}' not found. 1 pattern(s)      tried: ['solicitar/confirmar/(?P<id_pedido>\\d+)/(?P<cod_experto>\\d+)/$']

模板渲染admindata.html时出错,第74行出错:

<td><a href="{% url "usuario:entregado" id_pedido=ped.id cod_experto=ped.articulo.cod_experto  %}" method='GET' type="submit" class="btn btn-success pull-right" value="editar" onclick="document.location.reload();"/>Entregar</a></td>

全局网址:

urlpatterns = [
# Examples:
url(r'^solicitar/', include(urls, namespace="usuario")),
]

应用程序网址:

urlpatterns = [

url(r'^confirmar/(?P<id_pedido>\d+)/(?P<cod_experto>\d+)/$', login_required(Update_stock), name='entregado'),
]

和views.py:

def Update_stock(request, id_pedido, cod_experto):
if request.method == 'GET':
   pedido = Pedido.objects.get(id=id_pedido)
   articulo = Articulo.objects.get(id=cod_experto)
   articulo.stock -= pedido.cantidad
   stock.save()
return render(request, 'admindata.html', {'pedido':pedido, 'articulo':articulo})

最佳答案

您的 cod_experto 值为“ASE-0048”,它与仅匹配整数的正则表达式 \d+ 不匹配。

如果您希望能够接受这样的值,您需要更改您的正则表达式:

r'^confirmar/(?P<id_pedido>\d+)/(?P<cod_experto>[\w-]+)/$',

关于python - Django 的 NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859225/

相关文章:

django - 在 Django 中过滤 prefetch_related 为空

python - 属性错误 : module 'MySQLdb.constants.FIELD_TYPE' has no attribute 'JSON' while migrating in Django

django - Python 2.7 和 3.7.2 兼容的 django-redis 序列化器

python Pandas DF : adding to one column depending on the value in that row in another column

python - 如何在我的模块中确定主程序是否使用特定变量?

javascript - 实时应用程序 : Socket. io 与 Pusherapp

python - 从 Django Webserver 在远程服务器上运行脚本

python - 关于 np.tile 和 numpy 广播的困惑

python - 按列表的属性对列表进行排序

python - 在 Raspbian 上同时运行多个 python 版本