2015-08-31 34 views
2

下面描述的表單無效。和表達式{{ search_id_form.errors }}不顯示任何內容。 Django的模板:Django表單無效且沒有顯示任何錯誤

 <form method="POST"> 
      {% csrf_token %} 
      {{ search_id_form.idtype.label_tag }} 
      {{ search_id_form.idtype }} 
      {{ search_id_form.index.label_tag }} 
      {{ search_id_form.index }}<br> 
      <input type="submit" name="id_search_button" value="Submit"> 
     </form> 

Python類:

class IDSearchForm(forms.Form): 

    idtype = forms.ChoiceField(
     choices=[('idx', 'Our Database ID'), ('uprot', 'UniProt'), ('ncbi', 'NCBI')], 
     initial='idx', 
     widget=forms.RadioSelect, 
     label="Which identifier to use:" 
     ) 

    index = forms.CharField(label="Identifier:") 

查看:

def search(request): 
    if request.method == 'POST': 

     # handling other forms ... 

     # find a toxin by id 
     if 'id_search_button' in request.POST: 
      search_id_form = IDSearchForm() 
      if search_id_form.is_valid(): 
       idtype = search_id_form.cleaned_data['idtype'] 
       index = search_id_form.cleaned_data['index'] 
       return render(request, 'ctxdb/result_ctx.html', { 
        # here I try to use predefined object to pass to renderer (for debugging) 
        'ctx': get_object_or_404(CTX, idx='21') 
        }) 

      # handling other forms ... 

    # other forms 
    search_id_form = IDSearchForm() 
    # other forms 

    return render(request, 'ctxdb/search.html', { 
     # other forms 
     'search_id_form': search_id_form, 
     # other forms 
     }) 

在視圖功能我處理單個頁面上四種不同的形式。其他形式正常工作。這裏有什麼問題?

+1

向我們展示你的觀點,並提出申請後什麼在request.POST。 – beezz

回答

1

當您撥打.is_valid時,您需要將數據傳遞給search_id_form,這是您沒有做的。

變化

search_id_form = IDSearchForm() 

search_id_form = IDSearchForm(request.POST) 
相關問題