simultaneously submit three forms on the same page

67 views
Skip to first unread message

Devin Cky

unread,
Jul 29, 2014, 7:31:57 AM7/29/14
to django...@googlegroups.com
hi i am a beginner in django and I have a problem .. how can I validate 3 forms on the same page simultaneously ..please help me

Babatunde Akinyanmi

unread,
Jul 29, 2014, 7:47:51 AM7/29/14
to Django users

Hi Devin,

On 29 Jul 2014 12:33, "Devin Cky" <vin1...@gmail.com> wrote:
>
> hi i am a beginner in django and I have a problem .. how can I validate 3 forms on the same page simultaneously ..please help me
>

This is what formsets were made for.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/146660c0-083a-452b-b519-206c8bb2a1c2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Tom Evans

unread,
Jul 29, 2014, 9:48:00 AM7/29/14
to django...@googlegroups.com
On Tue, Jul 29, 2014 at 12:31 PM, Devin Cky <vin1...@gmail.com> wrote:
> hi i am a beginner in django and I have a problem .. how can I validate 3
> forms on the same page simultaneously ..please help me
>

If they are different types of forms, you cannot use a formset.

In that case, just test whether all forms are valid. If they are all
valid, save/process the forms. If not, re-render the page with the
bound forms and display the errors.

Eg

def myview(request):
if request.method == 'POST':
form_a = FormA(request.POST)
form_b = FormB(request.POST)
form_c = FormC(request.POST)
if form_a.is_valid() and form_b.is_valid() and form_c.is_valid():
form_a.save()
....
return HttpResponseRedirect(...)
else:
form_a = FormA()
form_b = FormB()
form_c = FormC()
return render(...)

Cheers

Tom

John

unread,
Jul 29, 2014, 10:51:43 AM7/29/14
to django...@googlegroups.com
How can one submit 3 forms simultaneously?? Even ajax, is on by one!

Sent from my iPhone

Santiago Lamora

unread,
Jul 29, 2014, 11:16:10 AM7/29/14
to django-users
2014-07-29 16:50 GMT+02:00 John <john.c...@gmail.com>:
How can one submit 3 forms simultaneously?? Even ajax, is on by one!


You can put several Django forms inside one <form> tag. To give each Form its own namespace, use the prefix keyword argument:

Devin Cky

unread,
Jul 29, 2014, 11:53:59 AM7/29/14
to django...@googlegroups.com, teva...@googlemail.com





-------thank you TOM---
  when I submit my forms .. no field is validated


.......view.....



def enregistrer(request):
        if request.method == 'POST':
            emet_form = EmetteurForm(request.POST, prefix = "emet")
            des_form = DestinataireForm(request.POST, prefix = "des")
            fich_form = FichierForm(request.POST, prefix = "fich")
            if emet_form.is_valid() and des_form.is_valid() and fich_form.is_valid():
                    civilite = emet_form.cleaned_data.get('civilite')
                    nom = emet_form.cleaned_data.get('nom')
                    prenom = emet_form.cleaned_data.get('prenom')               
                    cni = emet_form.cleaned_data.get('cni')
                    mail = emet_form.cleaned_data.get('mail')
                    telephone = emet_form.cleaned_data.get('telephone')   
                    pays = emet_form.cleaned_data.get('pays')
                    ville = emet_form.cleaned_data.get('ville')
                    quartier = emet_form.cleaned_data.get('quartier')
                    emet = emet_form.save()
                    nom = des_form.cleaned_data.get('nom')
                    prenom = des_form.cleaned_data.get('prenom')
                    mail = des_form.cleaned_data.get('mail') 
                    telephone = des_form.cleaned_data.get('telephone')
                    des = des_form.save()
                    description_fichier = fich_form.cleaned_data.get('description_fichier')
                    date_enregistrement = fich_form.cleaned_data.get('date_enregistrement')
                    observation = fich_form.cleaned_data.get('observation')
                    montant_prestation = fich_form.cleaned_data.get('montant_prestation')   
                    fich = fich_form.save()
        else:
            emet_form = EmetteurForm()
            des_form = DestinataireForm()
            fich_form = FichierForm()
                   
    return render(request,'formulaire/information.html',locals())            //***please look ..if my view is good***//
Message has been deleted

C. Kirby

unread,
Jul 29, 2014, 3:34:16 PM7/29/14
to django...@googlegroups.com, teva...@googlemail.com


They are not getting validated, or validation is failing?
Also, it looks like you are using model forms. You can get rid of all the for,.cleaned_data.get lines and just use:

if emet_form.is_valid() and des_form.is_valid() and fich_form.is_valid():
      emet = emet_form.save()
      des = des_form.save()
      fich = fich_form.save()

Devin Cky

unread,
Jul 30, 2014, 5:04:12 AM7/30/14
to django...@googlegroups.com, teva...@googlemail.com



thank you very much .. how is the template for such a view ... I've put each form in a form tag like that

<fieldset>
              

            <fieldset>            
                <legend>EMETTEUR</legend>
                    <form action=" " method="post">  {% csrf_token %}
                        {{ emet_form.as_p }}
                        <input type="reset" value="effacer" /> 
                   </form>
               </fieldset>

           

            <fieldset>            
                <legend>DESTINATAIRE</legend>
                    <form  action=" " method="post">  {% csrf_token %}
                        {{ des_form.as_p }}
                        <input type="reset" value="effacer" /> 
                   </form>
               </fieldset>


      
           <fieldset>            
                <legend>FICHIER</legend>
                    <form  action=" " method="post">  {% csrf_token %}
                        {{ fich_form.as_p }}
                        <input type="reset" value="effacer" /> 
                   </form>
               </fieldset>


             
                    <form  action="{%url 'formulaire.views.enregister'%}" method="post">  {% csrf_token %}
                     <input type="submit" value="enregistrer" /> 
                   </form>
          



</fieldset>

Tom Evans

unread,
Jul 30, 2014, 5:19:11 AM7/30/14
to django...@googlegroups.com
On Wed, Jul 30, 2014 at 10:04 AM, Devin Cky <vin1...@gmail.com> wrote:
>
>
>
>
> thank you very much .. how is the template for such a view ... I've put each form in a form tag like that

Only one <form> can be submitted at a time, so if you want all three
Forms to be submitted at the same time, they must all be inside one
<form>.

Cheers

Tom

Devin Cky

unread,
Jul 30, 2014, 5:29:54 AM7/30/14
to django...@googlegroups.com, teva...@googlemail.com



 I tried it .. but it is only the first form (   {{ emet_form.as_p }}    )  that is saved..


<form  action="{%url 'formulaire.views.enregistrer'%}" method="post">  {% csrf_token %}
                        {{ emet_form.as_p }}
                        {{ des_form.as_p }}
                        {{ fich_form.as_p }}                     
                   <input type="submit" value="record" />
         </form>

Tom Evans

unread,
Jul 30, 2014, 5:56:30 AM7/30/14
to django...@googlegroups.com
Please stop replying directly to me - I am subscribed to the list,
this is how we started talking, I do not need an additional copy in my
inbox.

On Wed, Jul 30, 2014 at 10:29 AM, Devin Cky <vin1...@gmail.com> wrote:
>
>
>
> I tried it .. but it is only the first form ( {{ emet_form.as_p }} )
> that is saved..
>
>
> <form action="{%url 'formulaire.views.enregistrer'%}" method="post"> {%
> csrf_token %}
> {{ emet_form.as_p }}
> {{ des_form.as_p }}
> {{ fich_form.as_p }}
> <input type="submit" value="record" />
> </form>
>

So, why is it only the first form that is saved? What happens in your
view that causes only the first form to be saved?

Add debug logging and find out.

Cheers

Tom

aRkadeFR

unread,
Jul 30, 2014, 3:32:42 PM7/30/14
to django...@googlegroups.com
Excellent.

Thanks for the share.

How backward compatible is it?
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEaW5xwy2cimDTTv%3DAUyfEeDrcK6xTtDfQ4AC1jXD-u%3DDNMU9A%40mail.gmail.com.

Santiago Lamora

unread,
Jul 31, 2014, 3:21:44 AM7/31/14
to django-users
2014-07-30 21:33 GMT+02:00 aRkadeFR <con...@arkade.info>:
Excellent.

Thanks for the share.

How backward compatible is it?

On 29/07/14 17:51, Santiago Lamora wrote:
> 2014-07-29 16:50 GMT+02:00 John <john.c...@gmail.com>:
>
> > How can one submit 3 forms simultaneously?? Even ajax, is on by one!
> >
> >
> Quotting
> https://docs.djangoproject.com/en/dev/ref/forms/api/#prefixes-for-forms
> > You can put several Django forms inside one <form> tag. To give each Form its
> own namespace, use the prefix keyword argument:
>


I think that is a pretty old feature. It's documented at least since django 1.3!!
Reply all
Reply to author
Forward
0 new messages