Python |如何在相同类别的模板中显示2个模型信息

时间:2020-06-26 11:26:35

标签: django django-models django-views

我需要在模板中获取2个模型信息。因为有些工作站具有设备。 (我需要在设备列表中显示Station的详细信息)如何在模板中显示2个模型的信息?

class StationDetailView(DetailView):
    model = Station
    model = Device
    template_name = "station/detail_station.html"

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

查看此链接,了解如何从Django文档中向细节视图类添加额外的上下文(模型) https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-display/#adding-extra-context

class StationDetailView(DetailView):
    model = Station
   
    template_name = "station/detail_station.html"

    def get_context_data(self, **kwargs):

        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of all the Device
        context['device_list'] = Device.objects.all()
        return context