我正在尝试在模型 Salle 上显示与外键相关的模型 Enigme 的值。
这些是类:
class Escape(models.Model):
name = models.CharField(max_length=100, unique=False)
def __str__(self):
return self.name
class Salle(models.Model):
salle = models.CharField(max_length=100, unique=False)
escape = models.ForeignKey(Escape, on_delete=models.CASCADE)
def __str__(self):
return self.salle
class Enigme(models.Model):
enigme_name = models.CharField(max_length=100, unique=False)
salle = models.ForeignKey(Salle, on_delete=models.CASCADE)
def __str__(self):
return self.enigme_name
我的查看文件:
def salle_escape(request):
escape_named = 'Test'
list_salle = Salle.objects.filter(escape__name=escape_named)
context = {
'escape' : Escape.objects.get(name=escape_named),
'salle' : list_salle,
}
return render(request, 'chat/salle_escape.html', context)
这是我的模板文件:
<!-- chat/templates/chat/room.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Room</title>
</head>
<body>
<h1>Test</h1>
<h2>Liste des salles</h2>
<ul>
{% for s in salle %}
<li>{{ s.salle }} </li>
<ul>
{% for enigme in s.enigme_set.all %}
<li>{{ engime.enigme_name }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</body>
</html>
您对我的问题有想法吗?
答案 0 :(得分:2)
{% for enigme in s.enigme_set.all %}
<li>{{ engime.enigme_name }}</li>
{% endfor %}
enigme!= engime
您错误地输入了对象的名称