Я создаю две формы, используя form wizard
.
Когда пользователь закончит заполнять first form(personal details)
, затем выберите следующую кнопку, он перейдет к second form(family member's details)
.
Когда пользователь выбирает кнопку отправки, данные сохраняются в определенную таблицу (Детали и Семейство).Мой движок базы данных - PostgreSQL.
Вот мой код:
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,
})
В таблице клиентов есть id
, name
, mobile
, email
и * 1017Атрибут * (внешний ключ), в котором таблица семейства имеет атрибут id
, name
и address
.
Ошибка вернулась:
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'
Но когда я попробовал это в customer_register.HTML, данные могут быть отображены:
{% for form in form_data %}
Name: {{ form.name }}
Mobile: {{ form.mobile }}
email: {{ form.email }}
......
{% endfor %}
ОБНОВЛЕНО
class CustomerForm(forms.Form):
name = forms.CharField(max_length=50,required=True)
mobile = forms.CharField(max_length=50,required=True)
email = forms.EmailField(max_length=50,required=True)
class FamilyForm(forms.Form):
famname = forms.CharField(max_length=50,required=True)
address = forms.CharField(max_length=50,required=True)