2016-04-29 24 views
-1

顯示我在我的模型兩類:Django的以html

class FicheFormation(models.Model): 
    intitule=models.TextField(blank=False,null=False) 
    duree=models.IntegerField(blank=False,null=False) 
    objective=models.TextField(blank=False,null=False) 
    niveau=models.TextField(blank=False,null=False) 
    prix=models.IntegerField(blank=False,null=False) 
    comptecf=models.ForeignKey('CompteRF',related_name='fichecompte',null=False) 
    unite= models.ManyToManyField('UniteFormation', related_name='unitefiche') 

class UniteFormation(models.Model): 
    nomUe=models.TextField(blank=False,null=False) 
    acronyme=models.TextField(blank=False,null=False) 
    commentaire=models.TextField(blank=True,null=True) 

,我想顯示comptecf的地層的名單,

,所以我寫的在我看來這種方法:

def getFormation(request): 
# user = request.user 
# if user and user.is_active: 
# u=user.username 
    c=CompteRF.objects.all().filter(username='Me') 
    queryset=FicheFormation.objects.all().filter(comptecf=c) 
    return render_to_response('formation.html', {'formations':queryset}, 
            context_instance=RequestContext(request)) 

在我formations.html:

% for car in formations %} 
      <tr> 
       <td>{{ car.intitule}}</td> 
       <td>{{ car.objective }}</td> 
       <td>{{ car.unite }}</td> 
      </tr> 
{% endfor %} 

更新:

{% for car in formations %} 
      <tr> 
       <td>{{ car.intitule}}</td> 
       <td>{{ car.objective }}</td> 
     % for car in formations %} 
       <td>{{ car.unite.nomUe }}</td> 
     {% endfor %} 
      </tr> 
{% endfor %} 

它適用於= car.intitule和car.objective但car.unite它顯示無,我不明白爲什麼因爲它正常地顯示統一'薩拉',在控制檯我需要做的:c = unite.all()然後c [0] .nomUe和它的作品和贊助薩拉'但在我看來或HTML當我嘗試迭代car.unite我有錯誤信息!

+0

什麼錯誤信息?你如何重複'car.unite'? –

+0

我所做的:%用於汽車的排兵佈陣%} ​​{{car.intitule}} ​​{{car.objective}}排兵佈陣% \t \t%用於汽車} ​​{{car.unite .nomUe}} \t \t {%ENDFOR%} {%ENDFOR%} – sarra

+1

請把那作爲更新的問題,以正確的格式。你似乎在兩次迭代「編隊」,而不是「團結」。 –

回答

1

要循環瀏覽相關的unite對象,您需要循環訪問car.unite.all()。在模板中,你放棄括號,所以你做類似的事情:

<td>{% for unite in car.unite.all %}{{ unite.nomUe }}{% endfor %}</td> 
+0

謝謝你的作品^^:D – sarra