Monday, May 4, 2015

Re: How to Process Multi Step Forms in Django?

Hi Ken,
You are getting this error because you are missing this line in your template: {{ formset.management_form }}

You need to carry the information to your second view. If you don't include first_name and last_address as fields in the second form, the information won't get to your second view, when you post. That's why I suggested rendering them as hidden inputs. Your Checkbox form also need to have the fields.

This is what I mean:
class NameForm (forms.Form):
first_name = forms.CharField (required = False)
last_name
= forms.CharField (required = False)

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


And on the template:

        <tr>
            
<td>{{ info.first_name }}</td>
            
<td>{{ info.last_address }}</td>
            
<td class="center"><input type="checkbox" name='overwrite' value="1"></td>
        
</tr>
        <input type="hidden" name="first_name" value="{{ info.first_name }}">
        <input type="hidden" name="last_address" value="{{ info.last_address }}">

Let me know if this helps.
cheers,

On Mon, May 4, 2015 at 3:50 PM, Ken Nguyen <eteknique@gmail.com> wrote:

Thank you for the input.  I've already tried what you've suggested but still the same result, "Management Form Data is Missing."  It's not going to know the "first_name" and "last_name" the second time around since I have the variable

{{ info.first_name }} and {{ info.last_name }}

My form2.html currently looking like so:

{% for info in data %}
       
<tr>
           
<input id="id_nameform-{{ forloop.counter0 }}-first_name" name="nameform-{{ forloop.counter0 }}-first_name" type="hidden" value="{{ info.first_name }}" />
       
<input id="id_nameform-{{ forloop.counter0 }}-last_name" name="nameform-{{ forloop.counter0 }}-last_name" type="hidden" value="{{ info.last_name }}" />
           
<td>{{ info.first_name }}</td>
           
<td>{{ info.last_name }}</td>
           
<td class="center"><input type="checkbox" name='overwrite' value="1"></td>
       
</tr>
       
{% endfor %}


When you say inherit from form1 and just add the checkboxes, can you elaborate that?  How do I inherit it?  Do you mean just replicate the first form and add checkboxes to it?

Thanks,

Ken



On Saturday, May 2, 2015 at 1:24:22 PM UTC-7, Bernardo Brik wrote:
You should add the same fields (first_name and address) to form2 and render them with hidden inputs.
You can try to make form2 inherit from form1 and just add the checkbox.

On Friday, May 1, 2015 at 9:58:28 PM UTC-3, Ken Nguyen wrote:

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 a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/jnDIOT4dNqQ/unsubscribe.
To unsubscribe from this group and all its topics, 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/797bdc71-28e4-4277-b645-7c0be9593b27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Bernardo Brik

--
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/CAO2FqvyTN3VkfKTxFAE8nEEXr_aEbP%2BEa2FfvaMTkMepJ8A3oQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment