如何在Django的数据库中保存表单向导数据的数据

时间:2019-09-12 16:21:18

标签: django django-models django-forms django-formwizard formwizard

我要分多个步骤添加个人资料页面,并在用户提交表单时将该页面添加到该页面,然后将该数据保存到django admin的数据库中

 ## forms.py ##     
    class FormStepOne(forms.Form):
        firstname = forms.CharField(max_length=100)
        lastname = forms.CharField(max_length=100)
        phone = forms. CharField(max_length=100)
        email = forms.EmailField()
        class Meta:
            model = FormStepOne
            fields = ['firstname', 'lastname', 'phone', 'email']

    class FormStepTwo(forms.Form):
        skills = forms.CharField(max_length=100)
        salary = forms.CharField(max_length=100)
        job_description = forms.CharField(widget=forms.Textarea)
        class Meta:

            model = FormStepTwo
            fields= ['skills','salary','job_description']


    ## views.py ##

    class FormWizardView(SessionWizardView):
    #
    #     def done(self,request, form_list, **kwargs):
    #         return render(self.request, 'accounts/online/profile.html', {
    #             'form_data': [form.cleaned_data for form in form_list],
    #         })
    #
    #         if request.method == 'POST':
    #             form = FormStepOne(request.POST)
    #             if form.is_valid():
    #                 form.save()
    #                 firstname = form.cleaned_data.get('firstname')
    #                 lastname=form.cleaned_data.get('last_name')
    #
    #                 messages.success(request, f'You successfully registered!')
    #                 return redirect('online_dashboard')
    #                 context={'form:from'}
    #         else:
    #             form = ()
    #         return render(request, 'accounts/online/firststep.html', {'form': form, 'title': ' Sign Up'})

这是我第一次使用stackoverflow,请帮助我解决此问题,并预先感谢

1 个答案:

答案 0 :(得分:0)

您的FormWizardView类需要定义a done method。向导的表单列表中的最后一个表单成功提交后,将触发该方法。在该方法内,您可以访问以向导管理的所有形式提交的所有数据,因此您可以执行任何必要的操作,以将这些数据实际写入那里。