I need to manually render my formset in my template and I cannot get the delete checkbox field into the template when I render manually. However, it does show when I render {{form.as_table}}.
views.py
QuoteManifestForm= modelformset_factory(QuoteManifest, QManifestForm, can_delete = True)
template - this does not display the {{form.DELETE}} but every other field shows fine, including id which I can see in the DOM.
{{ manifest.management_form }} <--!I passed QuoteManifestForm to template as 'manifest'-->
{% for form in manifest.forms %}
<div id="form_set">
<table id = 'manifest-table25' class="manifest-table2" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr class="manifest-row">
<td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form.ProductCode}}</td>
<td width = 32.5% class="description">{{form.DescriptionOfGoods}}</td>
<td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form.UnitQty}}</td>
<td width = 12.5% class="unitType">{{form.Type}}</td>
<td width = 12.5% class="price" oninput="calculate(this)">{{form.Price}}</td>
<td width = 12.5% class="amount2">{{form.Amount}}</td>
<td>{{form.DELETE}}</td>
{{form.id}}
</tr>
</tbody>
</table>
</div>
{% endfor %}
Any idea why that is not working?
UPDATE:
I found in the django docs that if you are rendering manually, you should include something like the below in your form. I tried this, but still no Delete field appears in my template:
{% if form2.can_delete %}
<td> {{form.DELETE}}</td>
{% endif %}
CODE TO REPRODUCE
views.py
def QuoteView(request):
QuoteManifestForm= modelformset_factory(QuoteManifest, fields =('ProductCode', 'DescriptionOfGoods', 'UnitQty', 'Type','Amount', 'Price'), can_delete = True)
if request.method == "POST":
form2 = QuoteManifestForm(request.POST)
form2.save()
return redirect('HomeView')
else:
form2 = QuoteManifestForm()
context = {
'form2': form2,
}
return render(request, 'quote.html', context)
quote.html
{{ form2.management_form }}
<div id="form_set">
{% for form2 in form2.forms %}
<table id = 'manifest-table25' class="manifest-table2" width=100%>
{% csrf_token %}
<tbody width=100%>
<tr class="manifest-row">
<td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form2.ProductCode}}</td>
<td width = 32.5% class="description">{{form2.DescriptionOfGoods}}</td>
<td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form2.UnitQty}}</td>
<td width = 12.5% class="unitType">{{form2.Type}}</td>
<td width = 10.5% class="price" oninput="calculate(this)">{{form2.Price}}</td>
<td width = 12.5% class="amount2">{{form2.Amount}}</td>
<td>{{form2.DELETE}}</td>
{{form2.id}}
</tr>
</tbody>
</table>
{% endfor %}
</div>
models.py
class QuoteManifest(models.Model):
ProductCode = models.ForeignKey(Product, null=True, blank=True)
DescriptionOfGoods = models.CharField(max_length=500, blank=True)
UnitQty = models.CharField(max_length=10, blank=True)
Type = models.CharField(max_length=50, blank=True)
Amount = models.CharField(max_length=100, blank=True)
Price = models.CharField(max_length=100, blank=True)
{{form2.DELETE}} is supposed to render a checkbox. This is what I cannot get working. It does work when I render the form as {{form2.as_p}} but that will not work for me in my case.