2016-11-25 54 views
-1

我瞭解Django的查詢集手柄Django的查詢集

這是我的看法

def CompletedFormulary(request) : 
# Fonction qui donne un récapitulatif des informations du formulaire par rapport au dernier formulaire créé. 

    # Reprise de tous les éléments de la table child et parent ayant l'ID le plus élevé 
    identity = Identity.objects.all().order_by("-id")[0] 

    identityAll = list(Identity.objects.all()) 
    context = { 
     "identity" : identity, 
     "identityAll" : identityAll, 
    } 

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

而且我recapitulatif_identity.html

<h2 align="center"> Votre formulaire a été validé </align> </h2> 


{% block content %} 
Votre personne porte le numéro : {{ identity.id }} 

<h3> Récapitulatif des données enregistrées : </h3> 

<li> Civilité : {{identity.title}}</li> 
<li> Nom : {{identity.lastname}}</li> 
<li> Prénom : {{identity.firstname}}</li> 
<li> Sexe : {{identity.sex}}</li> 
<li> Date de Naissance : {{identity.birthday}}</li> 
<li> Ville de Naissance : {{identity.birthcity}}</li> 
<li> Pays de Naissance : {{identity.birthcountry}}</li> 
<li> Nationalité : {{identity.nationality}}</li> 
<li> Profession : {{identity.job}}</li> 
<li> Adresse : {{identity.adress}}</li> 
<li> Ville : {{identity.city}}</li> 
<li> Code Postal : {{identity.zip}}</li> 
<li> Pays : {{identity.country}}</li> 
<li> Email : {{identity.mail}}</li> 
<li> Téléphone : {{identity.phone}}</li> 

<br></br> 

{{identityAll}} 

{% endblock %} 

<br></br> 

<form method='POST' action='/Identity/accueil'> {% csrf_token %} 
<input type ="submit" value="Retour fiche identité" /> 
</form> 

<form method='POST' action='/BirthCertificate/accueil'> {% csrf_token %} 
<input type ="submit" value="Création d'un acte de naissance" /> 
</form> 

的問題是有關從標識字段的顯示。我想打印每行每行,如果可能刪除[<Identity: ..... >

我不知道我該怎麼做。

這是結果

HTML page

+0

你有對象的列表,你需要在它們之間迭代(也是'名單()'是不必要的) – Sayse

+0

@Sayse你談論'

  • Civilité:{{identity.title}}
  • 'etc ...? – Deadpool

    +0

    'identityAll = list(Identity.objects.all())' - 查詢集已經可迭代 – Sayse

    回答

    3

    你只需要遍歷。

    <ul> 
    {% for item in identityAll %} 
        <li>{{ item }}</li> 
    {% endfor %} 
    </ul> 
    
    +0

    噢謝謝你!正是我想要的。我第一次做這種事情,所以我不完全理解哲學。 – Deadpool

    +0

    @ Andromedae93如果這正是你想要的,那麼你應該接受答案。 –

    +0

    @PrakharTrivedi我必須再等兩分鐘,我知道.. – Deadpool

    4

    只是做一個週期(見docs

    {% for i in identityAll %} 
    <p>{{i.title}}</p> 
    <!-- Any other property you want to add... --> 
    {% endfor %} 
    
    +1

    你是對的。將編輯。 – martinarroyo