可以在Django 1.6表单向导中重复步骤吗? [英] Can a step be repeated in Django 1.6 form wizard?

查看:52
本文介绍了可以在Django 1.6表单向导中重复步骤吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can a step be repeated in the Django form wizard? I'd like to repeat a step an indefinite number of times depending on the needs of the user.

推荐答案

文档包含有关如何进行条件步骤的说明.我将其与功能工厂一起使用来执行几百个条件步骤,因此从本质上讲,用户可以根据需要重复最后一步.

The documentation for the form wizard has directions on how to make conditional steps. I used that along with a function factory to make a few hundred conditional steps, so essentially the user can repeat the last step as many times as they want.

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
    ...

这篇关于可以在Django 1.6表单向导中重复步骤吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆