我已经在这个 Stackoverflow 帖子中使用 make_condition_stuff 函数实现了公认的答案只要按下“添加另一个”按钮或 True,它就可以重复 Django SessionWizard 步骤。我想为一个向导步骤添加这个函数,更具体地说,向导步骤 3。make_condition_stuff 函数返回一个自己的表单列表和一个条件字典,我想将它添加到向导表单列表和其他步骤的条件字典中。我不确定如何使用函数 make_condition_stuff 中的外部条件字典和表单列表在向导中添加或更新条件字典和表单列表。如何将函数中的外部 form_list 和条件字典与普通向导表单列表和条件字典集成?我试图在向导 self init中定义条件 dict 和 form_list并将其传递给 get_form_list 方法,但它引发了普通向导 form_list 所基于的 Ordered dict 错误,而从函数返回的 form_list 是普通列表,但是当我尝试将函数 form_list 附加到时引发类型错误带有向导 form_list 的 Ordered dict。我在这个 Stackoverflow 帖子中找到了 get_form_list 的类似实现,但我不确定如何在 self.form_list.keyOrder.index(step)+1 中实现 keyOrder。我还发现这个 Stackoverflow 帖子试图在 get_form 而不是 get_form_list 中更改表单顺序,但我不确定如何让它与 make_condition_stuff 函数一起使用,如果两个表单都应该切换并且条件 dict 和 form_list 应该是同时更新。
#in views.py
# function make_condition_stuff from Stackoverflow post answer
def function_factory(cond_step):
def info(wizard): # callable function called in _condition_dict
cleaned_data = wizard.get_cleaned_data_for_step(str(cond_step)) or {}
return cleaned_data.get("add another", True)
return info
def make_condition_stuff(extra, cond_step,form_lst):
cond_funcs = {}
cond_dict = {}
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]
return cond_funcs, cond_dict, form_lst
class Test_Wizard(SessionWizardView):
template_name = "contact_form.html"
model = ModelA
# the original solution takes the returned form
# list and condition_dict from the make_condition_stuff function;
_condition_dict = cond_dict
_form_list = form_list
# but I have a condition_dict for other
# steps and I only want to use the function on step 3 in the condition_dict;
_condition_dict = {'0': return_true,'1': check_condition_for_step_two,'2': return_true,'3':make_condition_stuff(extra, cond_step,form_lst) ,'4': return_true}
# I also have a form_list with several forms
# where I want to add the returned form_list from the make_condition_stuff function
_form_list = [FormA, FormB... plus returned form_list forms from function make_condition_stuff ]
# I've tried to pass the form_list and condition_dict through self to the method #get_form_list;
def __init__(self, **kwargs):
self.form_list = [FormA, FormB..]
self.condition_dict = {'0': return_true,'1': check_condition_for_step_two,'2': return_true,'3':make_condition_stuff(extra, cond_step,form_lst) ,'4': return_true}
return super(Test_Wizard, self).__init__(**kwargs)
def get_form(self, step=None, data=None, files=None):
form = super(Test_Wizard, self).get_form(step, data, files)
return form
def get_form_list(self):
form_data = self.get_cleaned_data_for_step(form_step) or {}
# if 'add another' is True, pass FormC with the 'add another' button at step 3 through the make_condition_stuff function
if 'add another' in form_data:
add_another = form_data.get('add another' )
if add_another == True:
form_lst = [FormC]
extra = 1
cond_step = 0
cond_funcs, cond_dict, form_list = make_condition_stuff(extra, cond_step,form_lst)
# update the wizard condition dict and form list with the returned form_list and condition dict from the function make_condition_stuff
#This is where I get a TypeError when trying to assign the form_list as self.form_list since it expects an Ordereddict as self.form_list and not a list in this method.
form_list = super(Test_Wizard, self).get_form_list()
return form_list