Django表单向导BooleanFields总是返回False

时间:2013-09-05 13:22:27

标签: python django forms wizard

首先,我通常从数据库创建我的表单。这是我的代码:

模板:

{% block wizard_form_content %}
<div id="alt-list">
    <div id="alt-list-header">
        <h4>Grids List</h4>
    </div>
    <div id="alt-list-data" class="container">
    {% for grid in data.grids %}
    <input type="checkbox" name="{{ grid.name }}" id="id_{{ grid.name }}" tabindex="{{ forloop.counter}}" size="30">{{ grid.name }}<br>
    {% endfor %}
    </div>
</div>
{% if wizard.form.errors %}
<div class="form-errors-wrapper">
    <div class="error">
    {% for error in wizard.form.non_field_errors %}
        <p>{{ error }}</p>
    {% endfor %}
    </div>
</div>
{% endif %}
<input type="hidden" name="num-grids" value="{{ data.grids|length }}" id="num-grids" />
<input type="hidden" name="user" value="{{ data.user }}" id="user" />
{% endblock wizard_form_content %}

这是相应的形式:

class WhichGridsForm(forms.Form):
#     Override the initialize in order to dynamically add fields to the form in order to be saved,
#     the fields are saved only when the user selects 'Next Step'.
    def __init__(self, *args, **kwargs):
        super(WhichGridsForm, self).__init__(*args, **kwargs)
        if len(self.data) > 0:
            self.num_grids = self.data['num-grids']
            user_name = self.data['user']

            user1 = User.objects.filter(username=user_name)

            gridtype = Grid.GridType.USER_GRID
            templateData = ShowGridsData()
            templateData.grids = Grid.objects.filter(user=user1, grid_type=gridtype)

            for grid in templateData.grids:
                gridName = grid.name
                # Every time, alternative fields are added with the name 'alternative..', and this because django
                # always adds '1-' % (where 1 the number of the step with zero index) prefix in the name,
                # with this the names are kept always the same.
                self.fields[gridName] = forms.BooleanField(required=False)

请记住,这是第2步,当我尝试使用以下代码行从步骤3获取此步骤2数据时:

elif self.steps.step1 == 3:
            try:
                grids_data = self.get_cleaned_data_for_step('1')
                print grids_data

即使我检查了所有字段,所有字段都显示为“False”。

{u'Cars': False, u'grid11': False, u'deneme11': False, u'asd': False}

你知道为什么会这样吗?

编辑:

但如果我在'done'方法中打印表单字段,我会得到正确的结果:

<MultiValueDict: {u'num-grids': [u'4'], u'deneme11': [u'on'], u'Cars': [u'on'], u'composite_wizard-current_step': [u'1'], u'grid11': [u'on'], u'user': [u'muratayan'], u'asd': [u'on'], u'csrfmiddlewaretoken': [u'JYIT5gHs35ZBvk7rCITfpMIPrFleUYXF']}>

1 个答案:

答案 0 :(得分:1)

抱歉,我给了你错误的田野课程。相反,MultipleChoiceField应该是ModelMultipleChoiceField,因为您从模型中进行选择。

这样的事情在我的案例中起作用:

forms.py(第一步表格)

class FirstStepForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(FirstStepForm, self).__init__(*args, **kwargs)
        self.fields['countries'] = forms.ModelMultipleChoiceField(queryset=Country.objects.all())

views.py

class MyWizard(SessionWizardView):
    def render(self, form=None, **kwargs):
        response = super(MyWizard, self).render(form, **kwargs)

        grids_data = self.get_cleaned_data_for_step('0') or {}
        print grids_data

        return response