django - 如何从 Profile 模型中获取图像到我的 Post 模型中
问题描述
我有两个模型Profile
和Post
. 我需要从Profile
模型到Post
模型访问图像。怎么做 ?
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='media')
#Some other fields
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
#Some other fields
解决方案
如果将Post
要渲染的对象传递给上下文,则可以使用以下方式渲染帖子作者的图像post
:
<img src="{{ post.user.profile.image.url }}">
推荐阅读
- java - JavaFX 圆形切口
- javascript - 为什么我在 Datatable JavaScript 中遇到 onClick 事件问题?
- python - 使用 A* 解决 8 谜题的堆栈溢出
- php - nginx 没有在 nextcloud 安装中解析 php
- c - 创建空堆栈时出现 C 错误
- mysql - 使用 sql 生成组合
- babeljs - babel 没有使用 .babelrc。为什么?
- java - 队列只有最后一条消息
- c# - C# 使用多个文本框过滤 DataGridView?
- msbuild - msbuild Dotfuscator exec 任务给出错误 MSB3073 exited with code 1