user3799942
user3799942

Reputation: 289

NoReverseMatch in django

I have this two urls:

url(r'^eliminar_cliente/(\d+)$', 'apps.Administracion.views.eliminar_cliente',name = 'eliminar_cliente'),
url(r'^eliminar_factura/(\d+)$', 'apps.Administracion.views.eliminar_factura',name = 'eliminar_factura'),

and this two buttons in different templates:

<button type="button" class="btn btn-danger"><a class="clientes" href="{% url 'eliminar_cliente' cliente.pk %}">Eliminar Cliente</a></button>

<button type="button" class="btnEliminar"><a class="clientes" href="{% url 'eliminar_factura' factura.pk %}">Eliminar Cliente</a></button>

And finally this two views:

def eliminar_cliente(request, id_cliente):
    instance = Cliente.objects.get(pk= id_cliente)
    instance.delete()
    messages.error(request, 'Document deleted.')
    return HttpResponseRedirect('../clientes')

def eliminar_factura(request, id_factura):
    instance = Factura.objects.get(pk = id_factura)
    instance.delete()
    return HttpResponseRedirect('../facturas')

As you can see they are both the same. This are two different buttons in different templates that do the same thing: delete a bill (Factura in spanish) and delete a Client.

The thing is that the template where i have to delete a Client works just fine, but the template where i have the button to delete a bill (Factura) is throwing this error:

NoReverseMatch at /Exi/ver_Factura/6
Reverse for 'eliminar_factura' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['Exi/eliminar_factura/(\\d+)$']

As both buttons have the same functionality i have NO IDEA why this is happening!

Any help will be really appreciated. Thanks

EDIT

This is the view thats having this problem

<section class="content">
<div class="row">
    <div class="col-xs-12">
        <div class="box">
           <div id="">
              <p id="address">
                  {{fact.nombre_cliente}}
              </p>
              <p id= "numero">
                  {{fact.numero_De_Factura}}
              </p>
              <div id="logo">
                  <img id="image" src="{% static 'img/Home/Logo-Exisoft.png' %}" alt="logo" />
              </div>
           </div>
           <div style="clear:both"></div>

           <div id="customer">
           <div id="datos">
               <p id = "direccion">
                   {{cliente.Direccion}}
               </p>
            <br>
               <p id = "direccion">
                {{fact.RI}}
               </p>
          </div>
          <table id="meta">
            <tr>
                <td class="meta-head">Fecha</td>
                <td><textarea id="date">{{fact.fecha_factura}}</textarea></td>
            </tr>
            <tr>
                <td class="meta-head">CUIT</td>
                <td><div class="due">{{cliente.CUIT}}</div></td>
            </tr>
         </table>
       </div>
       <table id="items">
           <tr>
               <th class="tipo">Tipo de Factura</th>
               <th class="descripcion">Descripcion</th>
               <th>Precio</th>
           </tr>
           <tr class="item-row">
              <td><div><textarea>{{fact.tipo_Factura}}</textarea></div></td>
              <td class="description"><textarea>{{fact.descripcion}}</textarea></td>
              <td><span class="price">$ {{fact.importe_sin_iva}}</span></td>
          </tr>
       </table>
       <table id="totales">
          <tr>
              <td class="total-line">Subtotal</td>
              <td class="total-value"><div id="subtotal">$ {{fact.importe_sin_iva}}                               </td>
          </tr>
          <tr>
              <td class="total-line">Iva</td>
              <td class="total-value"><div id="total">$ {{iva}}</div></td>
          </tr>
          <tr>
              <td  class="total-line">Precio Total</td>
              <td class="total-value"><textarea id="paid">$ {{total}}</textarea></td>
          </tr>
       </table>
       <div id="terms">
     </div>        
</div><!-- /.box-body -->
<div>

    <table>
        <td><button type="button" class="btnEliminar"><a class="clientes" href="{% url 'eliminar_factura' 1 %}">Eliminar Cliente</a></button></td>
    </table> 
    <form action="{% url 'descarga' fact.id %}">
        <input type="submit" name="_download" value="Descargar" id="buttonDescargar" class="btnDescargar"> 
    </form>       
</div>

EDIT 2

This is the view that renders the template where the button is:

@login_required
def ver_Factura(request, id_factura):
    fact = Factura.objects.get(pk = id_factura)
    cliente = Cliente.objects.get(factura = fact)
    template = 'ver_facturas.html'
    iva = fact.importe_sin_iva * 0.21
    total = fact.importe_sin_iva + iva

    extra_context = dict()
    extra_context['fact'] = fact
    extra_context['cliente'] = cliente
    extra_context['iva'] = iva
    extra_context['total'] = total

    return render(request,template, extra_context)

Upvotes: 0

Views: 368

Answers (2)

user2867522
user2867522

Reputation:

It is because factura.pk is None, as evident from the error message.

with arguments '('',)' and keyword arguments '{}' ...

Try hardcoding the ID for debugging purposes, and I'm sure it will work.

href="{% url 'eliminar_factura' 1 %}">

If that's the case, then try to figure out why factura or factura.pk is None.

Upvotes: 3

slim_chebbi
slim_chebbi

Reputation: 818

try to change urls to this:

url(r'^eliminar_cliente/(?P<id_cliente>\d+)$', 'apps.Administracion.views.eliminar_cliente',name = 'eliminar_cliente'),
url(r'^eliminar_factura/(?P<id_factura>\d+)$', 'apps.Administracion.views.eliminar_factura',name = 'eliminar_factura'),

Upvotes: 2

Related Questions