首页 > 解决方案 > ImageField 不适用于 Django 版本 2.0.4

问题描述

我正在使用 Django 2.0.4 版,但我遇到了models.ImageField.

每当我尝试用新图像更新现有图像时,什么都没有发生。此外,当我尝试清除图像时,什么也没有发生。我已经安装了所有的依赖项,比如 Pillow。

我已经像往常一样声明了我的 ImageField -

class Community(models.Model):
    image = models.ImageField(
        upload_to=upload_location,
        null=True,
        blank=True,
        width_field="width_field",
        height_field="height_field", help_text="A image representing the Community")
    height_field = models.IntegerField(null=True, blank=True)
    width_field = models.IntegerField(null=True, blank=True)

def __str__(self):
    return str(self.id)

我以前使用过 ImageField,但从未遇到过这样的问题。这是Django版本的问题吗?我应该考虑升级我的 Django 版本吗?提前致谢

更新

我尝试过的事情 - 我尝试过不同的数据库。尝试升级 Pillow 版本

标签: djangodjango-models

解决方案


您可以通过以下方式在 Django 中上传或更新图像:

模型.py 文件

class ImageModel(models.Model):
    image = models.FileField(upload_to="profile pics/", null=True, blank=True)

图像.html 文件

 <form method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
 </form>

这就是我过去在项目中上传/更新图像的方式。

希望一切顺利。干杯!


推荐阅读