django - 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 版本
解决方案
您可以通过以下方式在 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>
这就是我过去在项目中上传/更新图像的方式。
希望一切顺利。干杯!
推荐阅读
- python - 使用 jupiter notebook 在 conda 环境中设置 R
- c++ - QDebug 在 QDialog 中不起作用
- java - 如果顺序无关紧要,如何从具有 k 个元素的集合列表中创建具有 k+1 个元素的集合列表?
- python - 如何在 Python PyX 中隐藏轴文本
- ssis - SSIS 平面文件目标设置未保存(恢复为原始状态)
- javascript - 如何在 Angular 中有效取消提交表单?
- excel - 使用 VBA 将多个条码放置在一个单元格中,同时保留删除和撤消功能。
- angular - 更改角度组件内的活动引导选项卡
- java - 当用于全局变量初始化时,自动装配的 bean 给出了一个空指针异常
- javascript - 使用只有一个“搜索框”以及分页在多个列上应用搜索