2014-07-17 3 views
1

나는 꽤 바보하지만, 음 ... 나는이 두 가지 모델이이 질문을 가지고 :장고 템플릿에 두 가지 모델에서 데이터를 전달

class Cliente(models.Model): 
CUIT = models.CharField(max_length=50) 
Direccion = models.CharField(max_length=100) 
Razon_Social = models.CharField(max_length=100) 

def __unicode__(self): 
    return self.Razon_Social 

class Factura(models.Model): 
TIPO_FACTURA = (
    ('A', 'A'), 
    ('E', 'E') 
    ) 
tipo_Factura = models.CharField(max_length=1, choices= TIPO_FACTURA) 
nombre_cliente = models.ForeignKey(Cliente) 
fecha_factura = models.DateField() 
IRI = models.IntegerField() 
numero_De_Factura = models.IntegerField(max_length=50) 
descripcion = models.CharField(max_length=140) 
importe_Total= models.FloatField() 
importe_sin_iva = models.FloatField() 

def __unicode__(self): 
    return "%s - %s" % (unicode(self.nombre_cliente), self.numero_De_Factura) 
나는 각 클라이언트에서 청구서를 나열 해요

와 사용자가 클릭을

def verFactura(request, id_factura): 
    fact = Factura.objects.get(pk = id_factura) 
    cliente = Cliente.objects.filter(factura = fact) 
    template = 'verfacturas.html' 
    return render_to_response(template, locals()) 

임은 인포을 얻으려고 노력 :

이 내 views.py입니다 거기에 내가 법안에 대한 몇 가지 정보 (스페인어 Factura)과 ADRESS 같은 클라이언트에 대한 정보를 표시하려면^

URL (R '이 특정 법안의 클라이언트 기 때문에 내가 그 정보를 표시 할 수 있지만, 템플릿에 난 아무것도 참조 할수 없어 :

<div > 
    <p>{{fact.tipo_Factura}}</p> 
    <p>{{fact.nombre_cliente}}</p> 
    <p>{{cliente.Direccion}}</p> 
</div><!-- /.box-body --> 

그리고 이것은 내 URL입니다 verFactura/(\ D +) $ ','apps.Administracion.views.verFactura '이름 ='verFactura '),

사람 내가이 작업을 수행 할 수있는 방법을 말해 줄 수. 분명히 내 코드에 뭔가 잘못된 것이있어서 도움을 주시면 감사하겠습니다.

+0

제대로 코드 형식을 주시겠습니까? –

+0

Daniel에게 감사드립니다. 죄송합니다. – user3799942

답변

1

문제는 clienteCliente 예,하지만 인스턴스의 검색어되지 않는 것입니다 시도 사전에 감사합니다. 각 factura는 단일 cliente이있다, 그래서 당신은이 작업을 수행 할 수 있습니다

def verFactura(request, id_factura): 
    fact = Factura.objects.get(pk = id_factura) 
    cliente = Cliente.objects.get(factura = fact) # use `get` instead of `filter` 
    template = 'verfacturas.html' 

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

    return render_to_response(template, extra_context) 
2

def verFactura(request, id_factura): 
    fact = Factura.objects.get(pk = id_factura) 
    cliente = Cliente.objects.filter(factura = fact) 
    template = 'verfacturas.html' 

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

    return render_to_response(template, extra_context) 
+0

안녕하세요. 내 템플릿에서이 태그를 사용하고 있습니다 :

{{cliente.Direccion}}

아무 것도 반환되지 않습니다. 이것은 클라이언트 주소를 얻는 적당한 방법입니까?. 내가 제안한 코드로 views.py를 수정했습니다. 감사합니다. – user3799942

+0

예, 올바른 방법입니다. 'local()'또는'extra_context'가하고있는 것은 템플릿에서 사용할 수있는 키 값 쌍의 사전을 전달하는 것입니다. 클라이언트 개체가보기에서 없음이 아닌지 확인할 수 있습니까? –

관련 문제