可以在Django 1.6表单向导中重复一步吗?

时间:2014-03-31 19:56:53

标签: django formwizard

可以在Django表单向导中重复一个步骤吗?我想根据用户的需要无限次地重复一个步骤。

1 个答案:

答案 0 :(得分:2)

表单向导的documentation包含有关如何进行条件步骤的说明。我和函数工厂一起使用了几百个条件步骤,因此用户可以根据需要多次重复最后一步。

from django.contrib.formtools.wizard.views import SessionWizardView
from myapp.forms import BasicInformation, MoreInformation


def function_factory(cond_step):

    def info(wizard):
        cleaned_data = wizard.get_cleaned_data_for_step(str(cond_step)) or {}
        return cleaned_data.get("add_another_step", False)

    return info


def make_condition_stuff(extra, cond_step):
    cond_funcs = {}
    cond_dict = {}
    form_lst = [
        BasicInformation,
        MoreInformation,
    ]

    for x in range(extra):
        key1 = "info{0}".format(x)
        cond_funcs[key1] = function_factory(cond_step)
        cond_dict[str(cond_step+1)] = cond_funcs[key1]
        form_lst.append(MoreInformation)

    return cond_funcs, cond_dict, form_lst


last_step_before_extras = 1
extra_steps = 300

cond_funcs, cond_dict, form_list = make_condition_stuff(
    extra_steps,
    last_step_before_extras
)


class InfoWizard(SessionWizardView):
    form_list = form_list
    condition_dict = cond_dict
    ...