【发布时间】:2021-03-12 21:58:10
【问题描述】:
我正在尝试访问模型的子代并在父代详细信息页面上列出它们。这是我的设置方式...
型号:
class Destination(models.Model):
title = models.CharField( null=True, max_length=60, blank=True)
featuredimage = models.ImageField(null=True, blank=True, upload_to ='media/')
location = PlainLocationField(based_fields=['title'], zoom=7, null=True, blank=True)
class Airport(models.Model):
title = models.CharField( null=True, max_length=60, blank=True)
city = models.ForeignKey(Destination, null=True, blank=True, on_delete=models.SET_NULL)
观看次数:
def destination_detail(request, slug):
destination = Destination.objects.get(slug=slug)
context = {
'destination': destination,
'airport': Airport.objects.filter(city = destination.id),
}
return render(request,"destination/detail.html",context)
模板:
<h1>
{{ airport.title }}
</h1>
它不会抛出错误或任何东西,但不会显示任何内容。我已经导入并正确设置了所有内容,我想我只是缺少如何在我的视图中正确设置它。任何见解将不胜感激。
【问题讨论】: