Tuesday, July 29, 2014

Re: simultaneously submit three forms on the same page



On Tuesday, July 29, 2014 1:48:00 PM UTC, Tom Evans wrote:
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





-------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***//

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/3948ad1e-acbc-4185-a02d-ea5ed76717d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment