python - 如何从配置文件模型中获取图像
问题描述
如何从带有外键用户的配置文件模型链接中获取图像。我尝试{% for user in users %}{{ user.profile_pic }}{% endfor %}
使用 img src 这不显示图像。
class Profile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, null=True, blank=True)
profile_pic = models.ImageField(upload_to='ProfilePicture/', blank=True)
users = User.objects.exclude(id=request.user.id)
解决方案
我更像是烧瓶/SQLAlchemy 用户而不是 django 用户,但我相信原理非常相似。Profile
是映射到数据库的模型,profile_pic
是引用ImageField
类的属性,并且您的模板文件应该有一个调用,而不是ImageField
实例,而是对该实例的属性的调用,该实例存储图像文件的完整位置。因此,在您的示例中,调用将是这样的:
{% for user in users %}
<img src="{{ user.profile_pic.url }}">
{% endfor %}
我相信你也想使用filter
而不是exclude
在你的查询语句中,除非我误解了想要的结果。filter
将为您提供 id 变量与当前用户的 id 匹配的模型。exclude
将返回所有其他人,但不返回当前用户。
推荐阅读
- python - 如何使用正确的数据库参考设置 Flask-Migrate?
- mysql - 如何使用 SymmetricDS 将 MSSQL 表同步到 MYSQL 表
- azure - 如何将对象从触发器传递到编排和活动函数
- node.js - JSON 文件无法修改
- java - Twilio SMS api 不支持返回内容类型
- xslt - 如果其中包含特定的子字符串,则对 httpsamples 进行分组
- python - 十六进制输出总是乱码
- python-3.x - 如何在 Python 3.x 中查询 GCP 日志查看器并获取 json 结果(如 gcloud 日志读取)
- pandas - 熊猫按周分组并获得一天
- javascript - 如何获取 LG-WebOS 智能电视的 MAC 地址?