我正在创建一个发票应用程序。我希望能够在发票中添加一些项目。为此,该应用程序提供了两种模型:Invoice和InvoiceItem。我已将两种模型合并为一种形式。我现在想要实现的是能够在发票表格的表中添加一些项目。目前,我只能添加一项。我已经在表单中添加了一个按钮来添加新项目,但是s getting confusing and I wonder if there is a different way to achieve this. The button doesn
目前不起作用,表中的项目也是如此。
非常感谢,
models.py
class Invoice(models.Model):
TERMS = (
(0, '30 days'),
(1, '45 days'),
(2, '60 days'),
)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, on_delete=models.CASCADE, blank=True, null=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE, blank=True, null=True)
address = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
invoice_id = models.CharField(unique=True, max_length=6, null=True, blank=True, editable=False)
invoice_date = models.DateField(_("Date"), default=datetime.date.today)
invoiced = models.BooleanField(default=False)
class InvoiceItem(models.Model):
invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, blank=True, null=True)
description = models.CharField(max_length=100)
unit_price = models.DecimalField(max_digits=8, decimal_places=2)
quantity = models.DecimalField(max_digits=8, decimal_places=2, default=1)
vat = models.CharField(max_length=250)
total = models.CharField(max_length=250)
views.py
def create_invoice(request):
form = InvoiceForm(request.POST or None, request.FILES or None)
if request.method == 'POST':
invoice_form = InvoiceForm(request.POST)
invoiceitem_form = InvoiceItemForm(request.POST)
if invoice_form.is_valid() and invoiceitem_form.is_valid():
invoice_form.save()
invoiceitem_form.save()
return HttpResponseRedirect('/success')
else:
context = {
'invoice_form': invoice_form,
'invoiceitem_form': invoiceitem_form,
}
else:
context = {
'invoice_form': InvoiceForm(),
'invoiceitem_form': InvoiceItemForm(),
}
return render(request, 'invoices/create_invoice.html', context)
forms.py
class InvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
fields = ['project','invoice_date','address','invoiced']
class InvoiceItemForm(forms.ModelForm):
class Meta:
model = InvoiceItem
fields = ['description','quantity','unit_price',]
create_invoice.html
{% extends 'base.html' %}
{% block title %}New Invoice{% endblock %}
{% block invoices_active %}active{% endblock %}
{% load crispy_forms_tags %}
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-12 col-md-6">
<div class="jumbotron text-center" style="padding: 0.5em 0.6em; h1 {font-size: 2em;}">
<h1>New Invoice</h1>
</div>
<div class="card">
<div class="card-header">Invoice Details</div>
<div class="card-body">
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ invoice_form.as_p }}
<a href="" class="btn btn-sm btn-primary btn-create" role="button">New Item</button></a>
<table id="dtBasic" class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>Unit Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
//This is wrong below
***{% for invoiceitem in invoiceitems.all %}
<tr>
<td>{{ form.invoiceitem.id }}</td>
<td>{{ form.invoiceitem.description }}</td>
<td>{{ form.invoiceitem.unitprice }}</td>
<td>{{ form.invoiceitem.quantity }}</td>
</tr>
{% endfor %}***
</tbody>
</table>
{{ invoiceitem_form.as_p }}
<button type="submit"></button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}