Django find which form was submitted
Asked Answered
P

2

11

I have 3 formsets in a single template. Only one would be visible at a given time (the other two are hidden entirely):

<form style="display: none;">

All 3 forms are rendered with default values and should be valid even if no data was entered.

However, I would like to know which one was submitted when I validate the views.py.

In views.py I have the following:

def submitted(request):

    f1 = formset1(request.POST)
    f2 = formset2(request.POST)
    f3 = formset3(request.POST)

    if f1.is_valid() or f2.is_valid() or f3.is_valid():
        f1.save()
        f2.save()
        f3.save()
        # Do a lot of calculations...

        return render(request, 'submitted.html')

The problem is that I don't want f2 or f3 saved if only f1 was submitted (each formset has their own submit button). The '# Do a lot of calculations...' part is quite extensive and I don't want to replicate the code unnecessarily.

How can I use the same view, but only save and do the calculations for only the submitted formset?

Presser answered 7/4, 2015 at 8:22 Comment(2)
The ID of whichever submit button was clicked will be in request.POST.Shavonneshaw
Thanks for the quick response! I am not sure what you mean though - sorry! I tried viewing f1.id in the submitted.html and did not see anything?Presser
S
20

If each form has it's own submit button:

<form id='form1'>
    ...
    <input type='submit' name='submit-form1' value='Form 1' />
</form>
<form id='form2'>
    ...
    <input type='submit' name='submit-form2' value='Form 2' />
</form>
<form id='form3'>
    ...
    <input type='submit' name='submit-form3' value='Form 3' />
</form>

Then the name of the submit button will be in request.POST for whichever form was submitted:

'submit-form1' in request.POST # True if Form 1 was submitted
'submit-form2' in request.POST # True if Form 2 was submitted
'submit-form3' in request.POST # True if Form 3 was submitted
Shavonneshaw answered 7/4, 2015 at 8:33 Comment(0)
G
1

I have faced a similar issue where I had several forms in a template and I need to know which one was submitted. To do this I used the submit button.

Let's say you have the forms like this:

<form yourcode>
    # ... ...
    # your input fields
    # ... ... 
    <input method="post" type="submit" name="action" class="yourclass" value="Button1" />
</form>

<form yourcode>
    # ... ...
    # your input fields
    # ... ... 
    <input method="post" type="submit" name="action" class="yourclass" value="Button2" />
</form>

You can read the difference between those submit buttons, the value of the first is Button1 and the second is Button2

In your view you can have a control like this:

def yourview():
    # ... ... 
    # Your code
    # ... ... 
    if request.method == 'POST':
        if request.POST['action'] == "Button1":
            # This is the first form being submited
            # Do whatever you need with first form

        if request.POST['action'] == "Button2":
            # This is the second form being submited
            # Do whatever you need with second form

So, you can control which form is submitted using the value of the button. There is other ways to do that, but this way was the easier for my issue.

You can also use the name property to filter the forms

Gazzo answered 7/4, 2015 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.