2016-01-20 142 views
0

我在django向导窗体上遇到了一个很大的问题。跳过步骤x到步骤y并验证步骤x数据

我有3个步骤。第二步可以包含或不包含数据。最后一步是文件上传步骤。

在WizardForm类,我overrided的get_context_data方法,包括这在它:

if self.steps.current == 'against_indication': 
     questions = None 
     try: 
      # get the machine 
      machine_id = self.kwargs['pk'] 
      machine = Machine.objects.get(pk=int(machine_id)) 
      # check if there is against indications 
      if machine.type_question is False: 
       questions = YhappsQuestion.objects.filter(type_modalite=machine.type) 
      else: 
       questions = CustomQuestion.objects.filter(machine=machine) 
     except Machine.DoesNotExist: 
       pass 
     if len(questions) == 0: 
      # we modify the form wizard to skip against indication step 
      self.render_next_step(form, **kwargs) 
      #self.render_goto_step(step='against_indication', goto_step='prescription', **kwargs) 

正如你看到的,如果没有问题,我跳过第二步(against_indication)进入下一步骤(处方)。

问题出现在这里。当最后一步渲染时,向导窗体中没有足够的数据。在ddt的要求是这样的: with skip step。 因此,如果我上传文件,它会填充反对数据而不是处方数据,并重新渲染我最后一步...

我试图做所有这些,而不跳过第二步,看看如何看看ddt的要求: without skip step

有人有一个解决方案,允许有正确的数据时,我跳过步骤,PLZ?

感谢您的回答进一步

回答

1

我不认为get_context_data是在做这个正确的方法; FormWizard是一个非常具体的类,它限制了你可以执行不同功能的地方。

指定FormWizard跳过某个步骤时的典型方法是使用condition_dictionary。 Django使用该结构仅当条件(设置为可调参数)返回True时才包含该步骤的表单。如果不是,那么该步骤的表单不会强制调用form.is_valid(),绕过该步骤的验证。这也确保为每个步骤创建表单的所有隐藏管理信息。

这里有一个如何这可以工作例如:

# 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 YourFormWizard(SessionWizardView): 
    # Your form wizard itself; will not be called directly by urls.py, but rather wrapped in a function that provide the condition_dictionary 
    _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: 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), 
    ] 
    ... 


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 

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 == some_condition: 
     return True # show step 2 
    else: return False # or don't 

''' urls.py ''' 

your_form_wizard = YourFormWizard.as_view(YourFormWizard._form_list,condition_dict= YourFormWizard._condition_dict) 

urlpatterns = patterns('', 
    ... 
    url(r'^form_wizard_url/$', your_form_wizard, name='my-form-wizard',) 
) 
+0

谢谢@IanPrice!这个使用condition_dict的例子比官方的django例子更真实。它拯救了我的一天:) –

+0

很好听!是的,这些文档对FW来说确实不是很好。 –

+0

如果我能够帮助,将此标记为答案并upvoting是赞赏:) –