尽管initial = True,但未选中django复选框

时间:2018-02-20 15:56:58

标签: python django

我有一个Django表单,其中包含一个布尔字段“reduction”。该字段的初始值为True(即我希望相应的复选框呈现为已选中)。我的问题是,这不会发生。首次呈现表单时,将取消选中该复选框。

搜索此主题会产生大量与提交时复选框的值相关的问题,以及如果未选中它将如何发送任何内容等等。但我无法弄清楚为什么Django最初没有检查这个框。我已经验证了当表单从视图传递给模板时,表单中字段的值确实是真的。有任何想法吗?

表格:

class RunParamsForm(Form):
    fluid_present = BooleanField(widget=HiddenInput, required=False)
    solid_init_present = BooleanField(widget=HiddenInput, required=False)
    solid_sim_present = BooleanField(widget=HiddenInput, required=False)
    solid_present = BooleanField(widget=HiddenInput, required=False)
    load_map_present = BooleanField(widget=HiddenInput, required=False)
    scallop_map_present = BooleanField(widget=HiddenInput, required=False)
    fluid_endtim_units = CharField(widget=TextInput(attrs={'class': 'units', 'readonly': 'readonly'}))
    solid_init_drterm_units = CharField(widget=TextInput(attrs={'class': 'units', 'readonly': 'readonly'}))
    solid_sim_endtim_units = CharField(widget=TextInput(attrs={'class': 'units', 'readonly': 'readonly'}))
    steps = CharField(widget=HiddenInput)
    reduction = BooleanField(initial=True, required=False)

相关的html:

<!-- REDUCTION BLOCK -->
<div class="block1">
    <div class="queueRunHeading">
        Other
    </div>
    <div class="fieldWrapper">
        <label for="{{ form.reduction.id_for_label }}">
            1-Dimensional Reduction
        </label>
        {{ form.reduction }}
        <span class="errorlist">
            {{ form.reduction.errors.as_text }}
        </span>
    </div>
</div>

0 个答案:

没有答案