2017-09-06 76 views
0

我遇到了一個小問題,我不記得解決我的問題的方法。Django請求自動啓動

我有一個模板,它允許查詢數據庫並根據用戶的標準得到結果。

我的看法是這樣的:

@login_required 
def Identity_Individu_Researching(request) : 

    query_lastname_ID = request.GET.get('q1ID') 
    query_firstname_ID = request.GET.get('q1bisID') 
    query_naissance_ID = request.GET.get('q1terID') 

    sort_params = {} 

    set_if_not_none(sort_params, 'id__gt', query_lastname_ID) 
    set_if_not_none(sort_params, 'Prenom__icontains', query_firstname_ID) 
    set_if_not_none(sort_params, 'VilleNaissance', query_naissance_ID) 

    query_ID_list = Individu.objects.filter(**sort_params) 

    return render(request, 'Identity_Individu_Recherche.html', context) 

但這一要求被自動加載模板時啓動。

在我的HTML模板,我有:

<form autocomplete="off" method="GET" action=""> 
      <input type="text" name="q1ID" placeholder="Nom (ex:TEST) " value="{{ request.GET.q1ID }}"> et 
      <input type="text" name="q1bisID" placeholder="Prénom (ex:Test)" value="{{ request.GET.q1bisID }}"> &nbsp; 
      <input type="text" name="q1terID" placeholder="Ville Naissance" value="{{ request.GET.q1terID }}"> (optionnel) 
      <input class="button" type="submit" name="recherche" value="Rechercher">&nbsp; 
     </form> 

     <br></br> 

     <table style="width:120%"> 
      <tbody> 
       <tr> 
        <th>ID</th> 
        <th>État</th> 
        <th>N° Identification</th> 
        <th>Civilité</th> 
        <th>Nom</th> 
        <th>Prénom</th> 
        <th>Date de Naissance</th> 
        <th>Ville de Naissance</th> 
        <th>Pays de Naissance</th> 
        <th>Institution</th> 
       </tr> 
       {% for item in query_ID_list %} 
       <tr> 
        <td>{{ item.id}}</td> 
        <td>{{ item.Etat}}</td> 
        <td>{{ item.NumeroIdentification}}</td> 
        <td>{{ item.Civilite }}</td> 
        <td>{{ item.Nom }}</td> 
        <td>{{ item.Prenom }}</td> 
        <td>{{ item.DateNaissance }}</td> 
        <td>{{ item.VilleNaissance }}</td> 
        <td>{{ item.PaysNaissance }}</td> 
        <td>{{ item.InformationsInstitution }}</td> 
       </tr> 
       {% endfor %} 
      </tbody> 
     </table> 

所以,我怎麼能只啓動,如果用戶提交表單與表單按鈕查詢集?我知道它是基於name = "jhjh"

回答

2

您應該檢查是否請求包含表單數據。

if 'recherche' in request.GET: 
    ... 
+0

謝謝@DanielRoseman! – Deadpool

0

可以在模板

<form autocomplete="off" method="POST" action=""> 
<!--        ^^^^^  --> 

視圖改變方法表單:

query_ID_list = Individu.objects.all() 
if request.method == 'POST': 
    # your logic 
    query_ID_list = query_ID_list.filter(**sort_params) 

return render(request, 'Identity_Individu_Recherche.html', context) 
+0

POST通常不適用於搜索表單。 –

+0

會知道,謝謝。 –