[英]Using field of a related model in a django template
I have two models, Invoice
and Item
: 我有两个模型,
Invoice
和Item
:
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)
And these are the forms linked to the models: 这些是与模型链接的形式:
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')
My question is how to reference some field of the ItemForm
in the template? 我的问题是如何在模板中引用
ItemForm
某些字段?
Because I have no problems with the InvoiceForm. 因为我没有InvoiceForm的问题。 For example this works:
例如,这有效:
<!-- 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 views.py
def invoice_generator(request):
form = InvoiceForm
return render(request, 'invoiceapp/invoice_generator.html', {'form': form})
But I don't know how to reference for ItemForm, for example this doesn't work: 但是我不知道如何引用ItemForm,例如,这不起作用:
<div class="input-field col s5">
{{ form.description }}
</div>
In your views do like: 您认为:
def invoice_generator(request):
data = {}
data['invform'] = InvoiceForm()
data['itmform'] = ItemForm()
return render(request, 'invoiceapp/invoice_generator.html', data)
In your template: 在您的模板中:
<!-- 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.