如何从上一步输入的数据动态更改窗体实例

2024-10-06 09:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从表格中输入的数据来检查这个人是否存在于数据库中,如果是的话,我们将把登记表跳转到另一个表格中。唉,我的代码不起作用。有人能帮我

表格:

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

结果:页面显示时没有窗体


Tags: theselfformregisterdatagethtmlstep