2016-12-04 105 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是正确的!我感谢您的帮助 ! –