动态设置复选框的初始值

时间:2013-04-09 23:43:52

标签: django forms

我有一个带有CheckboxSelectMutliple小部件的MultipleChoiceField:

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"),
    ),
)

我尝试的是将7个复选框中的3个动态设置为" True"。理想情况下,我会从视图中做到这一点。

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,})

我知道如何使用ChoiceFields(在上面的例子中"目标"是一个ChoiceField),但是当涉及到MultipleChoiceFields时我会陷入困境。我真的很感激任何想法/反馈。

3 个答案:

答案 0 :(得分:4)

您可以将初始设置为列表。在这种情况下,如果您设置YourForm(inital={'weight_training_days': [0,1,2]}),则默认选择星期一,星期二和星期三。你也可以这样做forms.MultipleChoiceField(... inital=[0,1,2] ...)

答案 1 :(得分:1)

这很简单,只需传递清单即可。这是一个完整的例子:

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()

输出:

<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>

答案 2 :(得分:0)

OP表示,理想情况下,他们希望在视图中执行此操作,而且我经常发现您希望在视图级别而不是表单级别动态指定这些选项。

这是您在视图级别执行的操作

    form = MySelectForm()
    form.fields['my_choice_field'].initial = [1,2,3] 

您还可以通过以下方式动态指定选项(不仅是最初选择的选项):

    form.fields['my_choice_field'].choices = [1,2,3,4,5,6,7]