django - 蟒蛇 | 如何在具有相同类的模板中显示 2 个模型信息
问题描述
我需要在模板中获取 2 个模型信息。因为有些站点有设备。(我需要在设备列表中显示站点详细信息)如何在模板中显示 2 个模型信息?
class StationDetailView(DetailView):
model = Station
model = Device
template_name = "station/detail_station.html"
谢谢!
解决方案
查看此链接,了解如何从 django 文档 https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-display/#adding-向详细视图类添加额外的上下文(模型)语境外
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
推荐阅读
- fastlane - Fastlane Scan - macOS 上的默认输出目录
- python - Django 显示用户收藏页面
- javascript - 等待最后一条指令有用吗
- css - 有没有办法使用 CSS 删除或隐藏两个 HTML 元素之间的文本?
- sql - 一个 SQL 查询参数中的 SUM 和 MINUS
- amazon-web-services - AWS Cognito 和 DynamoDB,具有多对多关系的身份验证
- c++ - 如何修复 C++ 中的“二进制表达式的无效操作数”错误
- r - 在字符串检测中处理空字符串
- excel - 使用我的代码复制到下一个可用行
- documentum - 如何使用 DQL 通过 Samson 将文档委托给用户