12👍
import django
from django import forms
class MyForm(forms.Form):
option = forms.BooleanField(required=False, initial=True)
>>>print MyForm()
<tr><th><label for="id_option">Option:</label></th><td><input checked="checked" type="checkbox" name="option" id="id_option" /></td></tr>
>>> django.VERSION
(1, 3, 0, 'beta', 1)
>>>
As you can see the checked=”checked” is properly set.
Are you sure you are not modifying something with onload javascript ?
👤Davo
7👍
Set the attributes field:
options = forms.MultipleChoiceField(label='some label', choices=(('happy','Happy'),('sad','Sad')),
widget=forms.CheckboxSelectMultiple(attrs={'checked' : 'checked'}))
- How to tell if your select query is within a transaction or not?
- What is the usage of `FilteredRelation()` objects in Django ORM (Django 2.X)?
- Warnings (or even info messages) instead of only errors in Django
- Python Django Asynchronous Request handling
- Django and mysql problems on Mavericks
4👍
Try:
option = forms.BooleanField(
widget=forms.CheckboxInput(attrs={'checked': True})
)
Source:stackexchange.com