1

我使用 Django 1.4.8

我可以根据上一步的选择动态更改表单,但现在我需要更准确。

实际上,在我的表单“第 0 步”中,我有一个下拉列表,从中选择一个值,然后在第 2 步,我需要加载与“第 0 步”相同的数据,而在第 0 步没有选择的值。

我是这样做的:

urls.py

# wizard
url(r'^service/create/$',
    UserServiceWizard.as_view([ProviderForm,
                               DummyForm,
                               ConsummerForm,
                               DummyForm,
                               ServicesDescriptionForm]),

视图.py

def get_form(self, step=None, data=None, files=None):
    """
        change the form instance dynamically from the data we entered
        at the previous step
    """
    #
    if step is None:
        step = self.steps.current

    if step == '1':

        prev_data = self.get_cleaned_data_for_step('0')
        service_name = str(prev_data['provider']).split('Service')[1]
        class_name = 'th_' + service_name.lower() + '.forms'
        form_name = service_name + 'ProviderForm'
        form_class = class_for_name(class_name, form_name)
        form = form_class(data)

    elif step == '2':
        data = self.get_cleaned_data_for_step('0')
        form = ConsummerForm(initial={'provider': data['provider']})

        # EDIT 10/16 @ 10:00 
        # to track what's wrong I added 
        print form['consummer'].errors #return nothing 
        print form.is_valid() #return false
        print "form.non_field_errors ", form.non_field_errors() #return nothing


    elif step == '3':

        prev_data = self.get_cleaned_data_for_step('2')
        service_name = str(prev_data['consummer']).split('Service')[1]
        class_name = 'th_' + service_name.lower() + '.forms'
        form_name = service_name + 'ConsummerForm'
        form_class = class_for_name(class_name, form_name)
        form = form_class(data)

    else:
        form = super(UserServiceWizard, self).get_form(step, data, files)

    return form

我的表格是:

class ServiceChoiceForm(forms.Form):

    def activated_services(self, provider=None):
        """
        get the activated services added from the administrator
        """
        services = ServicesActivated.objects.filter(status=1)

        choices = []
        datas = ()

        if provider is not None:
            services = services.exclude(name__exact=provider)

        for class_name in services:
            datas = (class_name, class_name.name.rsplit('Service', 1)[1])
            choices.append(datas)

        return choices

class ProviderForm(ServiceChoiceForm):

    provider = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        super(ProviderForm, self).__init__(*args, **kwargs)
        self.fields['provider'].choices = self.activated_services()


class ConsummerForm(ServiceChoiceForm):

    consummer = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        super(ConsummerForm, self).__init__(*args, **kwargs)
        # get the list of service without the one selected in
        # the provider form
        self.fields['consummer'].choices = self.activated_services(
            self.initial['provider'])

现在发生了什么:

当我在“第 2 步”表格上并提交数据时......我无限期地返回“第 2 步”

如果我不将部分elif step == '2'放入get_form()并将相同的代码放入表单中,则 ConsummerForm :

self.fields['consummer'].choices = self.activated_services()

然后这个过程继续,但这不是我想要的:(

最后的事情 “=我注意到get_form()在“第0步”之后被调用了太多次:实际上是4次。我用print form,stepin跟踪了它,get_form()但形式从来都不是预期的。

如果有人能解释我如何修复向导中无限循环的行为;我会很感激的;)

4

1 回答 1

0

我找到了解决方案:

在第 2 步,我必须这样做

    elif step == '2':
        step0_data = self.get_cleaned_data_for_step('0')
        form = ConsummerForm(
            data, initial={'provider': step0_data['provider']})

代替

    elif step == '2':
        data = self.get_cleaned_data_for_step('0')
        form = ConsummerForm(initial={'provider': data['provider']})

因为在第一次我没有像我在第二段代码中偶然所做的那样覆盖“数据”字典......

现在一切正常!

于 2013-10-16T08:34:43.973 回答