What is the best way to handle different forms from one page in Django?

Let's say I have three different forms defined in my opinion:

# 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 its own unique field names, how do I handle all 3 forms from a single view? I was thinking about the following method, but I'm not sure if this is the best way to solve this problem:

# 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
source share
3 answers

I had a similar problem on the site I'm working on. I have no code with me right now, but I think I have done something in this direction:

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
0
source

- "" . :

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

, :

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

    <input type="submit" ... />
</form>
+4
  • My first thought: can you add '? form_id = 1 'to each action attribute.

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

    In view:

    form_id = request.GET.get('form_id', None)
    if form_id == '1':
        form_one = FormOne(request.POST)
    
  • Another option is to create separate URLs to submit.

    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)
        ...
    
  • You can check if there is a submit button name as you use 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:
        ...
    

+3
source

Source: https://habr.com/ru/post/1761519/


All Articles