Help with: NoReverseMatch at!

0

help with this error:

Reverse for 'pedido_sub' with arguments '()' and keyword arguments 
'{'cod_experto': 'AA-0002', 'id_pedido': 53}' not found. 
1 pattern(s) tried: 
['solicitar/aprobar/(?P<id_pedido>\d+)/(?P<cod_experto>\d+)$']
  • Mark me in red the index.html tag:
< a href="{% url "usuario:pedido_sub" id_pedido=ped.id cod_experto=ped.articulo.cod_experto %}" 
    type="submit"
  • The code for this button is found in views.py:
def pedido_sub(request, id_pedido, cod_experto):
    art = Articulo.objects.get(id=cod_experto)
    pedido = Pedido.objects.get(id=id_pedido)
    if request.method == 'GET':
        pedido.estado = 'entregado'
        pedido.save()
        pedido.fecha_entrega = datetime.now()
        pedido.save()
        art.stock = pedido.cantidad - art.stock
        art.save()
        return redirect('usuario:home')
    return render(request, 'index.html', {'pedido':pedido}, {'art':art})

What the def does, is to extract the id of the order and also the one of the article ( cod_experto ), button that modifies two fields, this does it without problems, but when incorporating the subtraction of the quantity field of Order table and stock table Article, this error occurs and I have not been able to correct it, if you can help me and advise me I would appreciate it very much.

    
asked by Demaro Create 17.02.2017 в 03:47
source

2 answers

1

A NoReverseMatch has to do with urls resolution, what you are indicating is that the url that you try to solve when you do

< a href="{% url "usuario:pedido_sub" id_pedido=ped.id cod_experto=ped.articulo.cod_experto %}" type="submit"

It does not match any of the ones defined, if you look closely at the definition of the same url that shows you the error

['solicitar/aprobar/(?P<id_pedido>\d+)/(?P<cod_experto>\d+)$'] tells you that the variable cod_experto has been defined as a digit (d + means you are waiting for one or more digits) therefore when passing the code 'AA-0002' does not match since it is receiving characters

I would recommend that you redefine your url in the following way

['solicitar/aprobar/(?P<id_pedido>\d+)/(?P<cod_experto>\w+)$']

Greetings

    
answered by 20.03.2017 / 14:31
source
0

I think the error is in the same subtraction, in:

art.stock = pedido.cantidad - art.stock
  • Surely the error is caused because the database has a condition to not accept values less than 0. If a = 2 and b = 5, a - b = -3, I imagine that there should be no less than 0 items in the stock, so the error is thrown at you.
  • possible solution:

art.stock -= pedido.cantidad

  • or equally

art.stock = art.stock - pedido.cantidad

I also wonder why you keep the order before and after the delivery date?

pedido.save()
pedido.fecha_entrega = datetime.now()
pedido.save()
    
answered by 17.02.2017 в 09:46