Why is my Django form run twice?

advertisements

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.

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?

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
            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')    # <====  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)

Update 1: 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.

For example:

def form2 (request):

    data = request.session ['data']
    n, errors = getInfo (data)    # <====  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)

Update 2: Since my explanation is a little long, allow me address Ale question here.

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.

context = {'data': data, 'errors': errors}

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.


The form2 view is run twice, once as a redirect from form1 which creates the form and renders the template, missing the if request.method == 'POST' part as this time around the request is a 'GET'.

When you submit form2 back to the same view method it prints the line you indicate again, this time the code in the if block executes as the request is a 'POST'.

The key is this line that redirects to the form2 view:

return HttpResponseRedirect ('form2')