2016-12-04 2 views
0

Wenn ich das Formular abschicke und es aus irgendeinem Grund nicht validiert wird, wird das Formular leer dargestellt. Ich benutze das {{form}} nicht, um das vollständige Formular zu rendern, ich lasse es anderen Leuten gerne zu, es anzupassen.Django - Halten Sie die Formularwerte nach der Formularvalidierung gefüllt

Dies ist ein Teil der Form:

<form method="post" action="" enctype="multipart/form-data">{% csrf_token %} 
       <div class="panel panel-default"> 
        <div class = "panel-heading"> 
         Informações de Contato 
        </div> 
        <div class = "panel-body"> 
         <div class="input-group"> 
          <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> 
          <input type="text" class="form-control" 
          id="id_{{ anuncioForm.nome_contato.name }}" 
          name="{{ anuncioForm.nome_contato.name }}" 
          value= "{{ request.user.first_name }} {{request.user.last_name}}" 
          placeholder="Nome"> 
         </div> 
         <p class="help-text">{{ anuncioForm.nome_contato.help_text }} </p> 
         <br> 

         <div class="input-group"> 
          <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> 
          <input type="text" class="form-control" 
          id="id_{{ anuncioForm.email_contato.name }}" 
          name="{{ anuncioForm.email_contato.name }}" 
          value="{{ request.user.email }} " 
          placeholder="E-mail"> 
         </div> 
         <p class="help-text">{{ anuncioForm.email_contato.help_text }} </p> 
         <br> 

         <div class="input-group"> 
          <span class="input-group-addon"><i class="glyphicon glyphicon-glyphicon glyphicon-phone"></i></span> 
          <input type="text" class="form-control" 
          id="id_{{ anuncioForm.telefone_contato.name }}" 
          name="{{ anuncioForm.telefone_contato.name }}" 
          placeholder="Telefone ou Celular"> 
         </div> 
         <p class="help-text">{{ anuncioForm.telefone_contato.help_text }} </p> 
        </div> 
       </div> 

Dies ist die view.py

def anunciar_imovel(request): 
    ImageFormSet = modelformset_factory(ImagensAnuncio, 
             form=ImagensForm, extra=3) 
    if request.method == 'POST': 
     anuncioForm = AnuncioForm(request.POST, request.FILES) 
     formset = ImageFormSet(request.POST, request.FILES, 
           queryset=ImagensAnuncio.objects.none()) 

     if anuncioForm.is_valid() and formset.is_valid(): 
      novo_anuncio = anuncioForm.save(commit=False) 
      novo_anuncio.user = request.user 
      novo_anuncio.save() 

      for form in formset.cleaned_data: 
       imagem = form['imagem'] 
       photo = ImagensAnuncio(anuncio=novo_anuncio, imagem=imagem) 
       photo.save() 
      return render(request, 'account/index.html') 
    else: 
     anuncioForm = AnuncioForm() 
     formset = ImageFormSet(queryset=ImagensAnuncio.objects.none()) 
    return render(request, 'imoveis/anunciar.html', {'anuncioForm':anuncioForm,'formset':formset }) 

Was könnte ich tun, um die gefüllten Werte zu halten?

+0

Ihr Code hat das von Ihnen gewünschte Verhalten. – e4c5

+0

Die Werte waren ein Test, aber ich war mir nicht sicher, ob es der richtige Ansatz war ... danke –

Antwort

0

Um das gewünschte Verhalten zu erhalten, müssen Sie die Werte aus anuncioForm verwenden:

<form method="post" action="" enctype="multipart/form-data">{% csrf_token %} 
    <div class="panel panel-default"> 
     <div class = "panel-heading"> 
      Informações de Contato 
     </div> 
     <div class = "panel-body"> 
      <div class="input-group"> 
       <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> 
       <input type="text" class="form-control" 
         id="id_{{ anuncioForm.nome_contato.name }}" 
         name="{{ anuncioForm.nome_contato.name }}" 
         value= "{{ anuncioForm.nome_contato.value or (request.user.first_name + ; ' + request.user.last_name) }}" 
         placeholder="Nome"> 
      </div> 
      <p class="help-text">{{anuncioForm.nome_contato.help_text }} </p> 
      <br> 
      <div class="input-group"> 
       <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span> 
       <input type="text" class="form-control" 
         id="id_{{ anuncioForm.email_contato.name }}" 
         name="{{ anuncioForm.email_contato.name }}" 
         value="{{ anuncioForm.email_contato.value or request.user.email }} " 
         placeholder="E-mail"> 
      </div> 
      <p class="help-text">{{ anuncioForm.email_contato.help_text }}</p> 
      <br> 
      <div class="input-group"> 
       <span class="input-group-addon"><i class="glyphicon glyphicon-glyphicon glyphicon-phone"></i></span> 
       <input type="text" class="form-control" 
         id="id_{{ anuncioForm.telefone_contato.name }}" 
         name="{{ anuncioForm.telefone_contato.name }}" 
         value="{{ anuncioForm.telefone_contato.value }}" 
         placeholder="Telefone ou Celular"> 
      </div> 
      <p class="help-text">{{ anuncioForm.telefone_contato.help_text }} </p> 
     </div> 
    </div> 
+0

Okay. Das war, was ich dachte, aber ich war mir nicht sicher, ob das der richtige Ansatz ist. Eigentlich mache ich jetzt die Validierung mit jquery, so dass die Seite nicht gerendert wird, wenn das Formular nicht gültig ist (gemäß meinen js-Regeln). Vielen Dank ! –

1

Ihrer Ansicht nach, wenn das Formular nicht gültig ein neues leeres Formular ist an die Vorlage gesendet werden. Sie müssen das Formular senden, das der Benutzer ausgefüllt hat, damit die Fehler richtig angezeigt werden können.

Sie müssen auch Ihre Vorlage anpassen, um die Fehler anzuzeigen - im Moment tut es das nicht.

die Ansicht zu beheben:

def anunciar_imovel(request): 
    ImageFormSet = modelformset_factory(ImagensAnuncio, 
             form=ImagensForm, extra=3) 
    anuncioForm = AnuncioForm(request.POST or None, request.FILES or None) 
    formset = ImageFormSet(request.POST or None, request.FILES or None, 
           queryset=ImagensAnuncio.objects.none()) 

    if anuncioForm.is_valid() and formset.is_valid(): 
     novo_anuncio = anuncioForm.save(commit=False) 
     novo_anuncio.user = request.user 
     novo_anuncio.save() 

     for form in formset.cleaned_data: 
      imagem = form['imagem'] 
      photo = ImagensAnuncio(anuncio=novo_anuncio, imagem=imagem) 
      photo.save() 

     return render(request, 'account/index.html') 

    return render(request, 'imoveis/anunciar.html', 
        {'anuncioForm':anuncioForm,'formset':formset }) 

die Vorlage zu beheben, müssen Sie, wenn es zu prüfen, selbst ein Fehler auf der formset und die individuellen Form ist. Ich überlasse diese Übung Ihnen als it is detailed in the documentation.

+0

Danke. Eigentlich habe ich beschlossen, es auf dem html (Aufruf der Feldwerte) zu behalten, aber Ihr Anwsser hat Recht! Ich schätze Ihre Hilfe ! –

Verwandte Themen