保存Django FormWizard

Nic*_*k B 5 forms django save

我一直在努力创建django FormWizard。我认为我已经很接近了,但是我无法弄清楚如何保存到数据库。

我尝试了这里建议的解决方案:

def done(self, form_list, **kwargs):
    instance = MyModel()
    for form in form_list:
        for field, value in form.cleaned_data.iteritems():
            setattr(instance, field, value)
    instance.save()

    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })
Run Code Online (Sandbox Code Playgroud)

但是将其放在done方法中会导致No Exception Supplied错误。save另一方面,将此代码放在方法中不会保存信息。

我也尝试过这里建议的解决方案:

def done(self, form_list, **kwargs):
    for form in form_list:
        form.save()
    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })
Run Code Online (Sandbox Code Playgroud)

但这会返回另一个错误:AttributeError at /wizard/ 'StepOneForm' object has no attribute 'save'。你遇到这个问题了吗?提交向导后如何将信息保存到数据库?谢谢

Fri*_*end 0

试试这个方法

def done(self,request,form_list):
    if request.method=='POST':
        form1 = F1articles(request.POST)    
        form2 = F2articles(request.POST)
        form_dict={}
        for x in form_list:
            form_dict=dict(form_dict.items()+x.cleaned_data.items())
        insert_db = Marticles(heading = form_dict['heading'],
        content = form_dict['content'],
        created_by = request.session['user_name'],  
        country = form_dict['country'],
        work = form_dict['work'])
        insert_db.save()
        return HttpResponse('data saved successfully')
Run Code Online (Sandbox Code Playgroud)