django中的NoReverseMatch

时间:2014-08-04 20:29:52

标签: python django django-views

我有两个网址:

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'),

和不同模板中的这两个按钮:

<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>

最后这两个观点:

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')

正如你所看到的,它们都是一样的。这是不同模板中的两个不同按钮,它们执行相同的操作:删除帐单(西班牙语中的Factura)并删除客户端。

问题是,我必须删除客户端的模板工作正常,但我有删除帐单的按钮(Factura)的模板抛出此错误:

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+)$']

由于两个按钮具有相同的功能,我没有想到为什么会发生这种情况!

任何帮助都将非常感激。感谢

修改

这是有这个问题的观点

<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>

编辑2

这是呈现按钮所在模板的视图:

@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)

2 个答案:

答案 0 :(得分:3)

这是因为factura.pkNone,从错误消息中可以看出。

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

尝试对ID进行硬编码以进行调试,我相信它会起作用。

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

如果是这种情况,请尝试找出facturafactura.pkNone的原因。

答案 1 :(得分:2)

尝试将网址更改为:

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'),