首页 > 解决方案 > 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)

标签: django

解决方案


推荐阅读