Django form wizard save data into PostgreSQL
NickName: Ask DateTime:2018-12-18T16:56:16

Django form wizard save data into PostgreSQL

I'm creating two form using form wizard.

When the user finish fill in the first form(personal details), then select next button, it will go to second form(family member's details).

When the user select submit button, the data will save into specific table(Details and Family). My database engine is PostgreSQL.

Here is my code in view:

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,
        })

Customer table have id,name,mobile,email,and familyid(Foreign Key) attribute where Family table have id,name and address attribute.

The error returned:

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'

But when i tried this in customer_register.html, the data can be display:

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

UPDATED

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)

Copyright Notice:Content Author:「」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53829405/django-form-wizard-save-data-into-postgresql

More about “Django form wizard save data into PostgreSQL” related questions

Django form wizard save data into PostgreSQL

I'm creating two form using form wizard. When the user finish fill in the first form(personal details), then select next button, it will go to second form(family member's details). When the user ...

Show Detail

Django form wizard save data into PostgreSQL

I'm creating two form using form wizard. When the user finish fill in the first form(personal details), then select next button, it will go to second form(family member's details). When the user ...

Show Detail

How to save files using django form wizard?

How to save files using django form wizard? I use Django 1.3 and i can't find examples and solutions. ;-/ With google and django docs i wrote this: class ContactWizard(FormWizard): def done(self,

Show Detail

Binding Files to Form Django Wizard

So I was unsuccessful at hooking up the Session-based wizard from django-merlin, but I am trying again with the wizard that is included in the django source. However, when trying to upload files us...

Show Detail

Django Form Wizard to Edit Model

I have a Django form wizard working nicely for creating content of one of my models. I want to use the same Wizard for editing data of existing content but can't find a good example of how to do th...

Show Detail

Conditional Form Field in Django wizard form

I am using wizard forms in django. I want to create a form field only if answer to some other form field is marked "yes" otherwise I don't want this new form field. How can I do this ? I have tried...

Show Detail

django wizard form problem in showing multiforms

My Views Page: from django.shortcuts import render from django.http import HttpResponse from formtools.wizard.views import SessionWizardView from .forms import ContactForm1,ContactForm2,ContactFor...

Show Detail

Django form-wizard form save

I would like to save the form and log in in a session wizard I used to do it using requests how would I use it like so>? Within the done function. class UserWizard(SessionWizardView):

Show Detail

Django Form Wizard not saving FileField

I have a 5 step form wizard, each their own form. I have a FileField in the first, but something is going wrong. When I reach the final step and press submit, my model gets saved but the file field...

Show Detail

Save Django Form wizard data to three models with related fields

I am working on a project that requires use of form wizard to populate three related models. The first model - Listing - has general data which has a OneToOneField relationship with the second model (

Show Detail