2014-07-17 59 views
1

我有這個問題,很愚蠢,但好...我有這樣兩種型號:Django的傳遞從兩個模型的數據到模板

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) 

我爲每個客戶端列出賬單,用戶點擊時它我想顯示有關該法案的一些信息(Factura西班牙語)和大約像其ADRESS客戶的一些信息

這是我的views.py:

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

我試着去獲得Informa公司這個特定法案的客戶端的重刑,所以我可以顯示其信息,但在模板我不能看到任何東西:

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

這是我的網址:

URL(R'^ VerFactura /(\ d +)$','apps.Administracion.views.verFactura',name ='verFactura'),

誰能告訴我怎麼做到這一點。顯然,我的代碼有問題,所以我會很感激這個幫助。謝謝你在前進

+0

能否請您正確格式化你的代碼? –

+0

謝謝丹尼爾。對不起,關於那 – user3799942

回答

1

的問題是,cliente不是Cliente實例,但實例的查詢集。每個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'正在做的是傳遞一個你可以在你的模板中使用的鍵值對的字典。 您是否可以檢查以查看您的客戶對象在視圖中是否不是無? –

相關問題