Set initial value of checkbox dynamically

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I have a MultipleChoiceField with a CheckboxSelectMutliple widget:

weight_training_days = forms.MultipleChoiceField(     help_text=u'(Required) 3 days must be selected',     widget=forms.CheckboxSelectMultiple(attrs={         'inline': True,     }),     choices=(         (0, "Mon"),         (1, "Tue"),         (2, "Wed"),         (3, "Thu"),         (4, "Fri"),         (5, "Sat"),         (6, "Sun"),     ), ) 

What I'm trying to is dynamically set 3 of the 7 checkboxes to "True". Ideally I would do this from the view.

def change_challenge_settings_page(request):      c = Challenge.objects.get(user__exact = request.user,chal_status=1)      layout = 'horizontal'     form =UpdateChallengeSettingsForm(initial={'goal': c.level_goal })       return render(request, 'portal/portal_change_challenge_settings.html', {'form': form,'layout': layout,'scorecard_page': True,}) 

I know how to do this with ChoiceFields (in the example above the "goal" is a ChoiceField) but am stuck when it comes to MultipleChoiceFields. I really appreciate any thoughts/feedback.

回答1:

You can set the initial to be a list. In this case if you set YourForm(inital={'weight_training_days': [0,1,2]}) it will default to having Monday, Tuesday and Wednesday selected. You can also do it like so forms.MultipleChoiceField(... inital=[0,1,2] ...)



回答2:

It's fairly simple, just pass a list. Here's a full example:

from django import forms class UpdateChallengeSettingsForm(forms.Form):     weight_training_days = forms.MultipleChoiceField(         help_text=u'(Required) 3 days must be selected',         widget=forms.CheckboxSelectMultiple(attrs={             'inline': True,         }),         choices=(             (0, "Mon"),             (1, "Tue"),             (2, "Wed"),             (3, "Thu"),             (4, "Fri"),             (5, "Sat"),             (6, "Sun"),         ),     )  form = UpdateChallengeSettingsForm(initial={     'weight_training_days': [1,2,4,5,6], })  print form.as_p() 

And the output:

<p><label for="id_weight_training_days_0">Weight training days:</label> <ul> <li><label for="id_weight_training_days_0"><input inline="True" type="checkbox" name="weight_training_days" value="0" id="id_weight_training_days_0" /> Mon</label></li> <li><label for="id_weight_training_days_1"><input checked="checked" name="weight_training_days" value="1" inline="True" type="checkbox" id="id_weight_training_days_1" /> Tue</label></li> <li><label for="id_weight_training_days_2"><input checked="checked" name="weight_training_days" value="2" inline="True" type="checkbox" id="id_weight_training_days_2" /> Wed</label></li> <li><label for="id_weight_training_days_3"><input inline="True" type="checkbox" name="weight_training_days" value="3" id="id_weight_training_days_3" /> Thu</label></li> <li><label for="id_weight_training_days_4"><input checked="checked" name="weight_training_days" value="4" inline="True" type="checkbox" id="id_weight_training_days_4" /> Fri</label></li> <li><label for="id_weight_training_days_5"><input checked="checked" name="weight_training_days" value="5" inline="True" type="checkbox" id="id_weight_training_days_5" /> Sat</label></li> <li><label for="id_weight_training_days_6"><input checked="checked" name="weight_training_days" value="6" inline="True" type="checkbox" id="id_weight_training_days_6" /> Sun</label></li> </ul> <span class="helptext">(Required) 3 days must be selected</span></p> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!