How to show a list of products as checkboxes?

1,666 views
Skip to first unread message

Michael Molloy

unread,
Jan 5, 2016, 11:39:31 PM1/5/16
to Django users
My model:

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 1 <input type=checkbox name=prodName1 value='Y'/>
Product 2 <input type=checkbox name=prodName2 value='Y'/>
Product 2 <input type=checkbox name=prodName3 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


Derek

unread,
Jan 7, 2016, 7:35:12 AM1/7/16
to Django users
You may want to look at Django crispy forms as a way to simplify form creation and layout;

For example, handling of checkboxes:

http://django-crispy-forms.readthedocs.org/en/latest/layouts.html#bootstrap-layout-objects

Derek

unread,
Jan 7, 2016, 7:35:29 AM1/7/16
to Django users
You may want to look at Django crispy forms as a way to simplify form creation and layout;

For example, handling of checkboxes:

http://django-crispy-forms.readthedocs.org/en/latest/layouts.html#bootstrap-layout-objects



On Wednesday, 6 January 2016 06:39:31 UTC+2, Michael Molloy wrote:

Michael Molloy

unread,
Jan 7, 2016, 9:02:43 PM1/7/16
to Django users
Thank you. crispy_forms are on my list, but I'm trying to learn Django's forms first.

--Michael

Michael Molloy

unread,
Jan 7, 2016, 9:04:13 PM1/7/16
to Django users

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 %}

Michael Molloy

unread,
Jan 7, 2016, 9:07:08 PM1/7/16
to Django users
Forgot to post the link to the guy who had this code: http://devdens.blogspot.com/2014/04/django-multiple-choice-field-and-how-to.html

Mario R. Osorio

unread,
Jan 8, 2016, 12:11:47 PM1/8/16
to Django users
Hmmm... not THE expert here , but it looks like your original question is not properly stated as you asked about checkboxes and did NOT mention radio buttons. They might look very much alike but are 2 very different controls, with very different behavior and uses.
Reply all
Reply to author
Forward
0 new messages