Django表单向导将数据保存到PostgreSQL

时间:2018-12-18 08:56:17

标签: django django-formwizard formwizard

我正在使用stringsAsFactors = FALSE创建两个表单。

用户完成填写form wizard后,选择“下一步”按钮,它将转到first form(personal details)

当用户选择提交按钮时,数据将保存到特定表中(详细信息和族)。我的数据库引擎是PostgreSQL。

以下是我的代码:

second form(family member's details)

客户表具有FORMS = [("customer", CustomerForm), ("family", FamilyForm)] TEMPLATES = {"customer": "customer_register.html", "family": "family_register.html",} def cust_contact_register(wizard): cleaned_data = wizard.get_cleaned_data_for_step('customer') or {'method': 'none'} return cleaned_data['method'] == 'family' class CustWizard(SessionWizardView): template_name = 'customer_register.html' # def get_template_names(self): # return [TEMPLATES[self.steps.current]] def done(self, form_list,form_dict,**kwargs): form_data= [form.cleaned_data for form in form_list] for form in form_data: name = form['name'] ## I want to get the data here to save nto database but it return error. mobile = form['mobile'] email = form['email'] familyname = form['familyname'] address = form['address'] add_member = Customer( name=name, mobile=mobile, email=email, ) add_family = Family( famname=famname, address=address, ) add_member.save() add_family.save() return render(self.request, 'customer_register.html', { 'form_data': form_data, }) idnamemobileemail(外键)属性,而家庭表具有familyididname属性。

错误返回:

address

但是当我在customer_register.html中尝试此操作时,可以显示数据:

Traceback:

File "C:\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python\Python36\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Python\Python36\lib\site-packages\formtools\wizard\views.py" in dispatch
  248.         response = super(WizardView, self).dispatch(request, *args, **kwargs)

File "C:\Python\Python36\lib\site-packages\django\views\generic\base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "C:\Python\Python36\lib\site-packages\formtools\wizard\views.py" in post
  309.                 return self.render_done(form, **kwargs)

File "C:\Python\Python36\lib\site-packages\formtools\wizard\views.py" in render_done
  365.         done_response = self.done(final_forms.values(), form_dict=final_forms, **kwargs)

File "C:\Users\customers\views.py" in done
  1543.             name= form['name']

Exception Type: KeyError at /customer/register/
Exception Value: 'name'

已更新

{% for form in form_data %}
    Name: {{ form.name }}
    Mobile: {{ form.mobile }}
    email: {{ form.email }}
    ......
{% endfor %}

0 个答案:

没有答案