开发者

pre-selected checkbox in django with django forms

开发者 https://www.devze.com 2023-02-17 14:30 出处:网络
Am trying to display a pre-selected checkbox in Django : option =forms.BooleanField(required=False, initial=True)

Am trying to display a pre-selected checkbox in Django :

option =  forms.BooleanField(required=False, initial=True) 

but the checkbox shows up un-checked. Am using django 1.3 beta. Am I miss开发者_如何学编程ing something here ?


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 ?


Set the attributes field:

  options = forms.MultipleChoiceField(label='some label',  choices=(('happy','Happy'),('sad','Sad')),
      widget=forms.CheckboxSelectMultiple(attrs={'checked' : 'checked'}))


Try:

option = forms.BooleanField(
            widget=forms.CheckboxInput(attrs={'checked': True})
        )
0

精彩评论

暂无评论...
验证码 换一张
取 消