Django图像没有文件关联,即使我有空白和null

时间:2019-05-25 19:30:07

标签: python html css django

当用户注册时,我曾经拥有它,以便将图像应用于该用户帐户。但是由于文件位置的原因,我再也无法拥有它了,所以我尝试拥有它,以便在用户注册时将图像字段留为空白,以便可以在模板上呈现伪默认图像。但是,当用户注册或查看那里的个人资料或登录时,会引发The 'image' attribute has no file associated with it.错误。我以为我可以使用blank=Truenull=True来解决这个问题,但是由于某些原因,它一直会抛出此错误。

型号:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(verbose_name='Profile Picture', upload_to='profile_pictures', blank=True, null=True)

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self, force_insert=False, force_update=False, using=None):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

观看次数:

def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST,
                                   request.FILES,
                                   instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been updated')
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    review = Post.objects.filter(live=False, author=request.user)
    post = Post.objects.filter(live=True, author=request.user)

    context = {
        'u_form': u_form,
        'p_form': p_form,
        'post': post,
        'review': review
    }

    return render(request, 'users/profile.html', context)

HTML:

{% if user.profile.image.url %}
                <div class="profile-image">
                    <img class="rounded-circle account-img" src="{{ user.profile.image.url }}" alt="{{ user.profile.image }}">
                </div>
                {% elif user.profile.image.url == None %}
                <div class="profile-image">
                    <img class="rounded-circle account-img" src="{% static '/public/images/default.jpg' %}" alt="{{ user.profile.image }}">
                </div>
                {% endif %}

如果任何人有任何类型的解决方案,那将很棒。

1 个答案:

答案 0 :(得分:0)

我认为您应该在个人资料的save方法中添加支票

def save(self, force_insert=False, force_update=False, using=None):
    super().save()

    if self.image:
        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

此外,您还必须检查模板中的image对象,然后访问url属性并进行预览。这就是方法。

{% if user.profile.image %}
    <div class="profile-image">
        <img class="rounded-circle account-img" src="{{ user.profile.image.url }}" alt="{{ user.profile.image }}">
    </div>
{% else %}
    <div class="profile-image">
        <img class="rounded-circle account-img" src="{% static '/public/images/default.jpg' %}" alt="{{ user.profile.image }}">
    </div>
{% endif %}