2015-12-25 167 views
0

我有兩個型號,InvoiceItem在Django模板使用相關模型的領域

class Invoice(models.Model): 
    vendor = models.CharField(max_length=200) 
    client = models.CharField(max_length=200) 
    number = models.CharField(max_length=200) 
    date = models.DateTimeField(max_length=200) 
    due_date = models.DateTimeField() 

    def __str__(self): 
     return "Invoice number: {}".format(self.number) 

class Item(models.Model): 
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE) 
    description = models.TextField() 
    quantity = models.DecimalField(max_digits=19, decimal_places=2) 
    rate = models.DecimalField(max_digits=19, decimal_places=2) 
    amount = models.DecimalField(max_digits=19, decimal_places=2) 
    subtotal = models.DecimalField(max_digits=19, decimal_places=2) 
    tax = models.DecimalField(max_digits=19, decimal_places=2) 
    notes = models.TextField() 
    terms = models.TextField() 

    def __str__(self): 
     return "{}".format(self.description) 

而這些都與模型的形式:

from django import forms 
from .models import Invoice, Item 

class InvoiceForm(forms.ModelForm): 

    class Meta: 
     model = Invoice 
     fields = ('vendor','client', 'number', 'date', 'due_date') 
     widgets = { 
      'date': forms.TextInput(attrs={'class':'datepicker'}), 
      'due_date': forms.TextInput(attrs={'class':'datepicker'}), 
     } 


class ItemForm(forms.ModelForm): 

    class Meta: 
     model = Item 
     fields = ('description', 'quantity', 'rate', 'amount', 
      'subtotal', 'tax', 'notes', 'terms') 

我的問題是如何引用模板中的ItemForm的某些字段?

因爲我對InvoiceForm沒有問題。例如這個工程:

<!-- Client --> 
<div class="row"> 
    <div class="input-field col s4"> 
    <label for="{{ form.client.id_for_label }}">Client</label> 
    {{ form.client }} 
    </div> 
    <div class="input-field col s4 offset-s4"> 
    <label for="{{ form.due_date.id_for_label }}">Due Date</label> 
    {{ form.due_date }} 
    </div> 
</div> 

views.py

def invoice_generator(request): 
    form = InvoiceForm 
    return render(request, 'invoiceapp/invoice_generator.html', {'form': form}) 

但我不知道如何爲ItemForm引用,例如這不起作用:

<div class="input-field col s5"> 
    {{ form.description }} 
</div> 
+1

我不明白,'ItemForm'和'InvoiceForm'沒有區別,你怎麼知道如何使用前者,但不知道如何使用後者? –

+0

你能發表你的看法嗎?你是否在上下文中傳遞了兩個表單? – jape

+0

在問題 –

回答

2

在您的觀點喜歡:

def invoice_generator(request): 
    data = {} 
    data['invform'] = InvoiceForm() 
    data['itmform'] = ItemForm() 
    return render(request, 'invoiceapp/invoice_generator.html', data) 

在您的模板中吃了:

<!-- Client --> 
<div class="row"> 
    <div class="input-field col s4"> 
     <label for="{{ invform.client.id_for_label }}">Client</label> 
     {{ invform.client }} 
    </div> 
    <div class="input-field col s4 offset-s4"> 
     <label for="{{ invform.due_date.id_for_label }}">Due Date</label> 
     {{ invform.due_date }} 
    </div> 
    <!-- Item form --> 
    <div class="input-field col s4 offset-s4"> 
     <label for="{{ itmform.due_description.id_for_label }}">Description</label> 
     {{ itmform.description }} 
    </div> 
</div> 
+0

中添加views.py標籤不應該是'{{invform.client.id_for_label}}'? –

+0

@JavierCárdenas是的......我已經更新了我的答案。 –