django - 如何在 html django 中引用外键
问题描述
{% for cred in allcreds %}
{% if cred.datasource.name == '...' %}
<h4>{{ cred.datasource }}</h4>
{% endif %}
{% endfor %}
在这种情况下,我检查条件中的数据源名称。然后它打印出数据源。我想有条件的数据源。
解决方案
我假设如果你打印出来{{ cred.protocoldatasource }}
它不会输出任何东西,因为你的“关系”protocoldatasource
不存在。
ProtocolUserCredentials
您的模型具有的可用外键是: protocol
, data_source
, user
, protocol_user
。
所以如果你这样做
{% if cred.data_source.name == 'Demonstration Protocol, ...' %}
或任何其他提到的关系,您可以访问您的相关模型。
另请注意,这{% if foo = 'bar' %}
是无效的,您需要==
在 if 语句中使用。
推荐阅读
- c++ - 无法链接 libtiff
- grails - 获取 Grails 2.* 中其他环境的配置
- java - 使用 HttpUrlconnection 登录和上传图片的问题
- c# - Xamarin Forms 应用程序在恢复时生成并发运行的应用程序
- r - 在 R 中,选择列表中的子列表并实际保留子列表的名称,而不是顶部父名称
- java - elasticsearch 6.3.2 的 NestedSortBuilder 使用示例
- go - 在 Go 中使用递归练习“裸返回”。我的 ELSE 语句中的 return 语句是否不正确?
- tfsbuild - TFS2018 构建 CI 批量更改触发器停止不执行
- apache - 如何自动添加 Apache HTTP vHost
- mysql - 如何在反应应用程序中从mysql数据库中的表中获取所有数据