2016-12-04 104 views
0

當我提交表單並且由於某種原因未驗證表單時,表單全部呈空白。我沒有使用{{form}}來呈現完整的表單,我喜歡讓其他人對其進行自定義。Django - 在表單驗證後填寫表單值

這是形式的一部分:

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

這個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 }) 

我應該怎麼做,以保持填充值?

+0

你的代碼有你正在尋找的bahaviour。 – e4c5

+0

這些值是一個測試,但我不知道這是正確的方法......謝謝 –

回答

0

爲了得到你想要的行爲,你必須從anuncioForm使用的值:

<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

好吧。這是我雖然但我不知道這是否是正確的方法。實際上,現在我正在使用jquery進行驗證,所以如果表單無效(根據我的js規則),頁面不會呈現。謝謝 ! –

1

在您看來,如果表單無效一個新的空白表單被髮送到模板。您需要發送用戶填寫的表格,以便可以正確顯示錯誤。

您還需要調整您的模板以顯示錯誤 - 現在它不這樣做。

要解決的觀點:

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

要解決的模板,你需要檢查是否有關於該formset和個人窗體本身的錯誤。我將這個練習留給你,如it is detailed in the documentation

+0

謝謝。其實我決定把它放在html上(稱爲字段值),但你的anwser是正確的!我感謝您的幫助 ! –