0

首先,我一般是从数据库中创建我的表单。这是我的代码:

模板:

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

即使我检查了所有字段,所有字段似乎都是“错误的”。

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

你知道为什么会发生这种情况吗?

编辑:

但是如果我在“完成”方法中打印表单字段,我会得到正确的结果:

<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']}>
4

1 回答 1

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

视图.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
于 2013-09-05T16:02:25.457 回答