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

会知道,谢谢。 –