class Product(models.Model):
descr = models.CharField(max_length=100)
cost = models.DecimalField(max_digits=5, decimal_places=2)
For every row stored in the table represented by this model, in the template I would like to render the description and a checkbox so that the user can check it if he wants to buy it.
Product 2 <input type=checkbox name=prodName3 value='Y'/>Product 1 <input type=checkbox name=prodName1 value='Y'/>
Product 2 <input type=checkbox name=prodName2 value='Y'/>
I can't figure out how to do that. I would be happy to manually render the form fields as checkboxes, but I don't know how to get the field id in the template.
For example,
{% for field in form %}
<div id="{{ field.auto_id }}_container">
<div>
<label for="{{ field.auto_id }}">{{ field. }}:</label> <input id="id_test1" name="test1" type="checkbox" />
</div>
<!--
<div id="id_test1_errors">
</div>
-->
</div>
{{ field.help_text }}
<div>
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
If this were java, I would write something like this in the jsp:
<c:forEach var="row" items="${products.result.rows}">
<input type=checkbox name='<c:out value=${row.id}/>' value='Y' /> <c:out value=${row.descr} /></c:forEach>
But I don't know how to do that with Python and Django.
And I may be coming at this totally wrong, so if that is the case, please feel free to tell me how I should be trying to do this.
Thank you,
--Michael
Found what I was looking for. In my form:
LINEITEM_CHOICES = [[x.id, x.descr] for x in LineItems.objects.all()]
class LineItemsForm(forms.Form):
food = forms.MultipleChoiceField(choices=LINEITEM_CHOICES,
widget=forms.CheckboxSelectMultiple(), required=False)
In my view:
{% for radio in lineItems.food %}
<div class="food_radios">
{{ radio }}
</div>
{% endfor %}