如何根据上一步输入的数据动态更改表单实例

时间:2019-05-04 16:26:48

标签: python django

我想从表单中输入的数据检查该人是否存在于数据库中(如果是),我们将注册表单跳过到另一个表单中。 my,我的代码无法正常工作。有人可以帮助我。

表格:

FORMS = [
         ("stage1-1", RegisterIdentification),
         ("stage1-2", RecRegister),
         ("stage2-1", BirthIdentification),
         ("stage2-2", RecBirth),
         ("stage3-1", FatherIdentification),
         ("stage3-2", RecFather),
         ("stage4-1", MotherIdentification),
         ("stage4-2", RecMother),
]

模板

TEMPLATES = {
        "stage1-1": "fr/public/births-wizard/stage1-1.html",
        "stage1-2": "fr/public/births-wizard/stage1-2.html",
        "stage2-1": "fr/public/births-wizard/stage2-1.html",
        "stage2-2": "fr/public/births-wizard/stage2-2.html",
        "stage3-1": "fr/public/births-wizard/stage3-1.html",
        "stage3-2": "fr/public/births-wizard/stage3-2.html",
        "stage4-1": "fr/public/births-wizard/stage4-1.html",
        "stage4-2": "fr/public/births-wizard/stage4-2.html",
}

观看次数:

class BirthsWizard(SessionWizardView):
instance = None
def get_form(self, step=None, data=None, files=None):
    """
        change the form or the step instance dynamically from the data we entered
        at the previous step
    """
    # get the default form
    form = super(BirthsWizard, self).get_form(step, data, files)

    if step is None:
        step = self.steps.current

    if step == 'stage1-2':
        # get prev datas and chek if they are in Database
        step1_data = self.get_cleaned_data_for_step('stage1-1')
        register_doctype = step1_data.get('registerDoctype')
        register_nationality = step1_data.get('registerNationality')
        register_idNum = step1_data.get('registeridNum')
        if register_doctype and register_nationality and register_idNum:
            check_register = BirthsDeclarer.objects.filter(doctype=register_doctype,
                                                           nationality=register_nationality,
                                                           idNum=register_idNum)
            if check_register:
              # if  prev datas are in Database jump to a specific step
                form_name = "stage2-1"
                form = form_name

    return form


def get_template_names(self):
    return TEMPLATES[self.steps.current]

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
    return super(BirthsWizard, self).dispatch(*args, **kwargs)




def done(self, form_list, **kwargs):
    # i will process data here 
    return render(self.request, 'fr/public/births-wizard/done.html', {'form_data': [ form.cleaned_data for form in form_list ] })

Result: the page  displays without the form

结果:页面不显示表格

0 个答案:

没有答案