【问题标题】:Can a step be repeated in Django 1.6 form wizard?可以在 Django 1.6 表单向导中重复一个步骤吗?
【发布时间】:2014-03-31 19:56:52
【问题描述】:

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

【问题讨论】:

标签: django formwizard


【解决方案1】:

表单向导的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
    ...

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2018-06-18
    • 2011-06-30
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多