Django表单向导使用条件逻辑跳过多个步骤
Django的表单向导(即SessionWizardView)中是否有跳过多个步骤的方法 我知道可以使用条件dict生成以下表单显示逻辑: 第1页->第3页Django表单向导使用条件逻辑跳过多个步骤,django,django-forms,django-formwizard,Django,Django Forms,Django Formwizard,Django的表单向导(即SessionWizardView)中是否有跳过多个步骤的方法 我知道可以使用条件dict生成以下表单显示逻辑: 第1页->第3页 #urls.py path('main/', MyWizard.as_view(set_of_forms, condition_dict={'1': view_condition} )) 我想做的是: 第1页-->第4页 大概,根据第1页的内容添加一个条件dict应该可以跳过第3页,但它不起作用。例如: #urls.py path('
#urls.py
path('main/', MyWizard.as_view(set_of_forms, condition_dict={'1': view_condition} ))
我想做的是:
第1页-->第4页
大概,根据第1页的内容添加一个条件dict应该可以跳过第3页,但它不起作用。例如:
#urls.py
path('main/', MyWizard.as_view(set_of_forms, condition_dict={'1': view_condition, '2': view_condition2,} ))
我真的被弄糊涂了,不知道该如何破解这个坚果。非常感谢您提供的任何指导。指定FormWizard何时跳过步骤的典型方法是使用条件字典。Django使用该结构仅在条件(设置为callables)满足时包含步骤的表单
保罗,谢谢你的回答!这正是轻松跳过表单页面所需要的。根据Paulo的回答,需要进行三个更改以同时跳过多个步骤:
# I always specify index values for steps so that all functions can share them
STEP_ONE = u'0'
STEP_TWO = u'1'
STEP_THREE = u'2'
def MyWizard(SessionWizardView):
# Your form wizard itself; will not be called directly by urls.py, but rather wrapped in a function that provide the condition_dictionary
# **Change1**: functions need to be stated before the dictionary
def return_true(wizard): # callable function called in _condition_dict
return True # a condition that is always True, for when you always want form seen
# **Change 2:** Only proceed with the logic if the step is valid
def check_step_two(wizard): # callable function called in _condition_dict
step_1_info = wizard.get_cleaned_data_for_step(STEP_ONE)
# do something with info; can retrieve for any prior steps
if step_1_info != None:
if step_1_info == some_condition:
return True # show step 2
else: return False # or don't
# **Change 3**: a condition must be added to skip an additional form
_condition_dict = { # a dictionary with key=step, value=callable function that return True to show step and False to not
STEP_ONE: return_true, # callable function that says to always show this step
STEP_TWO: check_step_two, # conditional callable for verifying whether to show step two
STEP_THREE: check_step_two, # conditional callable for verifying whether to show step three
STEP_FOUR: return_true, # callable function that says to always show this step
}
_form_list = [ # a list of forms used per step
(STEP_ONE,your_forms.StepOneForm),
(STEP_TWO, your_forms.StepTwoForm),
(STEP_THREE, your_forms.StepThreeForm),
(STEP_THREE, your_forms.StepFourForm),
]
...
''' urls.py '''
your_form_wizard = MyWizard.as_view(MyWizard._form_list,condition_dict= MyWizard._condition_dict)
urlpatterns = patterns('',
...
url(r'^form_wizard_url/$', your_form_wizard, name='my-form-wizard',)
)
谢谢你,@PauloRodrigues!你的回答很清楚,也很有帮助。我已经重新发布了你的答案,其中有一些小改动,说明了如何跳过多个步骤。
# I always specify index values for steps so that all functions can share them
STEP_ONE = u'0'
STEP_TWO = u'1'
STEP_THREE = u'2'
def MyWizard(SessionWizardView):
# Your form wizard itself; will not be called directly by urls.py, but rather wrapped in a function that provide the condition_dictionary
# **Change1**: functions need to be stated before the dictionary
def return_true(wizard): # callable function called in _condition_dict
return True # a condition that is always True, for when you always want form seen
# **Change 2:** Only proceed with the logic if the step is valid
def check_step_two(wizard): # callable function called in _condition_dict
step_1_info = wizard.get_cleaned_data_for_step(STEP_ONE)
# do something with info; can retrieve for any prior steps
if step_1_info != None:
if step_1_info == some_condition:
return True # show step 2
else: return False # or don't
# **Change 3**: a condition must be added to skip an additional form
_condition_dict = { # a dictionary with key=step, value=callable function that return True to show step and False to not
STEP_ONE: return_true, # callable function that says to always show this step
STEP_TWO: check_step_two, # conditional callable for verifying whether to show step two
STEP_THREE: check_step_two, # conditional callable for verifying whether to show step three
STEP_FOUR: return_true, # callable function that says to always show this step
}
_form_list = [ # a list of forms used per step
(STEP_ONE,your_forms.StepOneForm),
(STEP_TWO, your_forms.StepTwoForm),
(STEP_THREE, your_forms.StepThreeForm),
(STEP_THREE, your_forms.StepFourForm),
]
...
''' urls.py '''
your_form_wizard = MyWizard.as_view(MyWizard._form_list,condition_dict= MyWizard._condition_dict)
urlpatterns = patterns('',
...
url(r'^form_wizard_url/$', your_form_wizard, name='my-form-wizard',)
)