Friday, May 1, 2015

How to Process Multi Step Forms in Django?

I've made some attempted to use formwizard but there wasn't much documentation about it so I decided to stay with the basic. I've successfully obtained and display the data from the first form to the second form and added some checkbox next to the data to allow user to choose whether to overwrite or ignore the duplicate data found in the backend process. The problem I have is the second form doesn't know how retrieve the data of the first form after hitting "Confirm" button. The form2.html template invalidated the data completely since it called itself again by the form action after submitting the data. Is there a way to solve this or a better approach to this?

forms.py

class NameForm (forms.Form):
first_name = forms.CharField (required = False)
last_name
= forms.CharField (required = False)

class CheckBox (forms.Form):
overwrite = forms.BooleanField (required = False)

views.py

def form1 (request):

   
NameFormSet = formset_factory (NameForm, formset = BaseNodeFormSet, extra = 2, max_num = 5)

   
if request.method == 'POST':

        name_formset
= NameFormSet (request.POST, prefix = 'nameform')

       
if name_formset.is_valid ():
            data
= name_formset.cleaned_data

            context
= {'data': data}
           
return render (request, 'nameform/form2.html', context)
       
else:
            name_formset
= NameFormSet (prefix = 'nameform')

     context
= {......}

     
return render (request, 'nameform/form1.html', context)

def form2 (request):

   
CheckBoxFormSet = formset_factory (CheckBox, extra = 2, max_num = 5)

   
if request.method == 'POST':

        checkbox_formset
= CheckBoxFormSet (request.POST, prefix = 'checkbox')

       
if checkbox_formset.is_valid ():
            data
= checkbox_formset.cleaned_data

            context
= {'data': data}
           
return render (request, 'nameform/success.html', context)

       
else:
            checkbox_formset
= CheckBoxFormSet (prefix = 'checkbox')

     
return HttpResponse ('No overwrite data.')



form2.html


<!DOCTYPE html>
<html>
<head lang="en">
   
<meta charset="UTF-8">
    {% load staticfiles %}
   
<link rel="stylesheet" type="text/css" href="{% static 'nodeform/style.css' %}" >
   
<title>User Information</title>
</head>
<body>
   
<h1>User Information:</h1>
   
<form action="form2" method="POST">
   
<div id="tablefont">
   
<table id="table01">
       
<tr>
           
<th>First Name</th>
           
<th>Last Name</th>
           
<th class="center">Overwrite</th>
       
</tr>
        {% for info in data %}
       
<tr>
           
<td>{{ info.first_name }}</td>
           
<td>{{ info.last_address }}</td>
           
<td class="center"><input type="checkbox" name='overwrite' value="1"></td>
       
</tr>
        {% endfor %}
   
</table>
   
</div>
   
<br>
   
<p><input type="submit" value="Confirm">
   
<a href="{% url 'form1' %}">
       
<button type="button">Cancel</button></a></p>
   
</form>
</body>
</html>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/27760adf-7d0d-4544-a645-72a7969c58a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment