I am tring to create drop down checkbox with values from database in my templet using bootstrap select and form MultipleChoiceField like this
Html
select class="selectpicker" multiple>
{% for topping in form.the_topping %}
<option value="{{ topping }}">{{ topping.topping_id }}</option>
{% endfor %}
</select>
<script type="text/javascript">
$(document).ready(function() {
$('.selectpicker').selectpicker({
style: 'btn-info',
size: 4
});
});
</script>
Form.py
class ProfileForm(forms.ModelForm):
the_topping =
forms.ModelMultipleChoiceField(queryset=Topping.objects.all(), required=False, widget=forms.CheckboxSelectMultiple)
class Meta:
model = Pizza
exclude = ['user']
the bootstrp working and the form is working but doesnot combined
can any one help me PLS
You have to properly construct the <select> element.
Take a look at these sections of the manual:
https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually
https://docs.djangoproject.com/en/1.10/topics/forms/#looping-over-the-form-s-fields
Take a special look at field html_name; you should be using that as the name of your select element. The name is used to map the posted data to the form in the code.
Camilo.