2017-09-06 11 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> 

私は、ユーザーがフォームのボタンでフォームを送信する場合にのみ、クエリセットを起動することができますので、どのように?私はそれはあなたがビューにテンプレート

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

でフォームのための方法を変更することができますname = "jhjh"

答えて

2

は、あなたがチェックする必要があります。

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

@DanielRosemanありがとうございます! – Deadpool

0

に基づいています知っている:要求はフォームデータが含まれている場合

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

ご存知でしょうか、ありがとうございます。 –

関連する問題