2016-01-20 183 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是讚賞:) –