title: Why is My Django Form Executed Twice?
body: <p>Can someone explain to me why form 2 executed twice?  In another word, I would see 2 print statements, "Hello from form 2," in the console.</p>
  
  <p>The first print statement occurred after I clicked "Submit" from form 1.  Second print statement comes after the second "Submit" I clicked from form 2.  How do I make it to only print once?</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>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
              request.session ['data'] = data
  
              return HttpResponseRedirect ('form2')
          else:
              name_formset = NameFormSet (prefix = 'nameform')
  
       context = {'name_formset': name_formset}
  
       return render (request, 'nameform/form1.html', context)
  
  
  def form2 (request):
  
      data = request.session ['data']
      print ('Hello from form 2')    # &lt;====  This statement printed twice in the console
  
      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 ():
              for i, form in enumerate (checkbox_formset.cleaned_data):
                  data [i].update (form)      # Join cleaned data with original data
  
              del request.session ['data']
              context = {'data': data}
              return render (request, 'nameform/success.html', context)
  
              checkbox_formset = CheckBoxFormSet (prefix = 'checkbox')
  
       context = {'checkbox_formset': checkbox_formset, 'data': data}
       return render (request, 'nameform/form2', context)
  </code></pre>
  
  <p><strong>Update 1:</strong>
  The "print" statement is actually a backend method that processes the data obtained from form 1 and display it in form 2.  Leaving where it is now would cause that method to process the information twice.  I have no issue or error doing it this way but it's unnecessary.</p>
  
  <p>For example:</p>
  
  <pre><code>def form2 (request):
  
      data = request.session ['data']
      n, errors = getInfo (data)    # &lt;====  This statement performed twice in the console
      if request.method = 'POST':
      ....    
          if checkbox_formset.is_valid ():
              for i, form in enumerate (checkbox_formset.cleaned_data):
                  data [i].update (form)      # Join cleaned data with original data
  
              n.process_new_data (data, errors)
              del request.session ['data']
  
              context = {'data': data, 'errors': error}
              return render (request, 'nameform/success.html', context)
  
      else:
          checkbox_formset = CheckBoxFormset (prefix = 'checkbox')
  
      context = {'data': data, 'errors': error}
      return render (request, 'nameform/form2.html', context)
  </code></pre>
  
  <p><strong>Update 2</strong>:
  Since my explanation is a little long, allow me address Ale question here.</p>
  
  <p>Yes, I fully understand why it processed twice.  To briefly answer your question, putting getInfo inside 'POST' will give me a context, unbound error because of the context "errors" dictionary doesn't exist in the first redirect.</p>
  
  <pre><code>context = {'data': data, 'errors': errors}
  </code></pre>
  
  <p>I'd to update my post so that I can explain why I can't use your method.  GetInfo takes the data from form1, processes it, and passes it on to form 2 to display.  I could do all that in form1 but then I would have to redo it in form2 because form2 will not know what 'n' or 'errors' is without passing it through sessions.  I'm just trying to see if there's a better way to do this.</p>