How To Make Pre-selected Checkbox In Django?
My form has CheckboxSelectMultiple widget receiving user's preferences. I want to show a pre-checked preference to user. I know there is one way to pre-check widget in the templat
Solution 1:
Just pass the values you want to be checked to initial
(see docs) when you instantiate the form:
MyForm(initial={
'my_multi': ['a', 'b', 'c']
})
Solution 2:
import django
from django import forms
classMyForm(forms.Form):
option = forms.BooleanField(required=False, initial=True)
It renders so
<tr><th><labelfor="id_option">Option:</label></th><td><inputchecked="checked"type="checkbox"name="option"id="id_option" /></td></tr>
Post a Comment for "How To Make Pre-selected Checkbox In Django?"