views:

51

answers:

3

Let's say I have the 3 different forms defined in my view:

# views.py
form_one = FormOne()
form_two = FormTwo()
form_three = FormThree()

In my template:

<form action="" method="post" id="form-one">
  {{ form_one.as_table }}
  <input type="submit" value="Submit Form One" name="form-one" />
</form>

<form action="" method="post" id="form-two">
  {{ form_two.as_table }}
  <input type="submit" value="Submit Form Two" name="form-two" />
</form>

<form action="" method="post" id="form-three">
  {{ form_three.as_table }}
  <input type="submit" value="Submit Form Three" name="form-three" />
</form>

Assuming each form has their own unique field names, how do I handle all 3 forms from one view? I was thinking of the following method but I'm not sure if it's the best way to tackle this issue:

# views.py

if request.method == 'POST':
    request_post = request.POST
    if 'form-one' in request_post:
        form_one = FormOne(request.POST)
    elif 'form-two' in request_post:
        form_two = FormTwo(request.POST)
    else:
        form_three = FormThree(request.POST)
else:
    form_one = FormOne()
    form_two = FormTwo()
    form_three = FormThree()

Any comments or suggestions?

+3  A: 
  1. My first thought is you can add a '?form_id=1' to each forms action attribute.

    <form action="?form_id=1" method="post" id="form-one">
    

    In the view:

    form_id = request.GET.get('form_id', None)
    if form_id == '1':
        form_one = FormOne(request.POST)
    
  2. Another option would be to create separate urls to post to.

    in urls.py

    url(r'^form1/$', 'someapp.views.process_forms', {'form_class': FormOne}, name='form_one_page'),
    url(r'^form2/$', 'someapp.views.process_forms', {'form_class': FormTwo}, name='form_one_page'),
    url(r'^form3/$', 'someapp.views.process_forms', {'form_class': FormThree}, name='form_one_page'),
    

    in views.py:

    def process_forms(request, form_class=None):
        ...
        form = form_class(data=request.POST)
        ...
    
  3. You can check if submit button name since your using them in your submit button.

    if request.POST.has_key('form-one'):
        ...
    elif request.POST.has_key('form-two'):
        ...
    elif request.POST.has_key('form-three'):
        ...
    else:
        ...
    

Louis
A: 

I had a similar problem in a site I'm working on. I don't have the code with me right now but I think I did something along these lines:

if request.method == 'POST': 
    valid = False
    form = FormOne(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormOne
        valid = True

    form = FormTwo(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormTwo
        valid = True

    form = FormThree(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormThree
        valid = True

    if not valid:
        #handle case where none of the forms were valid
theycallmemorty
+1. This is what I would do. This also allows you to avoid multiple `<form>` tags in the HTML and submit to the same view instead. One _possible_ change: second and third `if` branches can be converted to `elif` and the last `if` can then become `else`.
Manoj Govindan
The only problem I see here is if each Form has similar form fields, then FormOne may also be validate FormTwo and so on.
Louis
+2  A: 

The correct way to handle multiple forms is by using the "prefix" attribute when you create your forms. This is how your view should looks like:

if request.method == 'POST':
    form1 = Form1(request.POST, prefix='form1')
    form2 = Form2(request.POST, prefix='form2')
    form3 = Form3(request.POST, prefix='form3')

    if form1.is_valid() and form2.is_valid() and form3.is_valid():
        # Do whatever you have to do
        pass

else:
    form1 = Form1(prefix='form1')
    form2 = Form2(prefix='form2')
    form3 = Form3(prefix='form3')

The template remains the same, no extra logic needed in there:

<form ...>
    {{ form1.as_table }}
    {{ form2.as_table }}
    {{ form3.as_table }}

    <input type="submit" ... />
</form>
Cesar Canassa