django - Django 个人资料图片:“ImageField”属性没有与之关联的文件
问题描述
感谢您抽出宝贵时间!这是我的问题,我正在做一个关于 Django 博客的小项目。当我上传个人资料图片时,一切正常,但当我不上传时,“'ImageField' 属性没有与之关联的文件。” 错误提示。问题是我在静态目录中上传了一个默认图片并创建了,如果要管理它。如果用户没有照片,只需显示静态目录中的图像。这是 HTML 和模型文件。
<div class="ui items">
<div class="item">
{% if post.author.profile.image.url %}
<div class="ui small image">
<img src="{{ post.author.profile.image.url }}">
</div>
{% else %}
<div class="ui small image">
<img src="{% static 'static/ProyectoBlog/images/default-profile-pic.png' %}">
</div>
{% endif %}
<div class="content">
<a class="header">{{ post.author.first_name }} {{ post.author.last_name }}</a>
<div class="meta">
<span>About</span>
</div>
<div class="description">
<p>{{ post.author.profile.bio }}</p>
</div>
<div class="extra">
<a href="{{ post.author.profile.fb_url }}">
<i class="big facebook icon"></i>
</a>
<a href="{{ post.author.profile.tw_url }}">
<i class="big twitter icon"></i>
</a>
<a href="{{ post.author.profile.ig_url }}">
<i class="big instagram icon"></i>
</a>
</div>
</div>
</div>
</div>
class Profile(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
bio = models.TextField(max_length=500)
image = models.ImageField(upload_to="bios", blank=True, null=True, default='static/ProyectoBlog/images/default-profile-pic.png')
fb_url = models.CharField(max_length=255, null=True, blank=True)
tw_url = models.CharField(max_length=255, null=True, blank=True)
ig_url = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return str(self.user)
解决方案
推荐阅读
- mysql - REGEXP_REPLACE 无法按预期工作以替换用逗号包裹的字符
- python - reduce() 和 Reduce()(大写和小写)
- unicode - 无法在文件标题上插入 Unicode 字符
- android - 如何在 MVP Android 中向 Model 类注入上下文?
- python - 有断开按钮和连接按钮?
- docker - 为什么 Ubuntu docker 映像不是 VM
- javascript - 无法使用 admin sdk 获取 Firestore 文档
- php - PHP MySQL - 在数据库中的两个单独的表上获取相同的 ID
- php - 如何在下拉列表中显示当前年份和明年类似 2018-2019 的内容?
- java - 本机查询问题