我试图通过管理站点上传一个.svg到SQLite(django的默认数据库),我得到了以下错误:
Upload a valid image. The file you uploaded was either not an image or a corrupted image.
我可以上传.jpg文件,它工作正常。
class News(models.Model):
news_id = models.AutoField(primary_key=True, editable=False)
news_title = models.CharField(max_length=150)
news_date = models.DateTimeField(auto_now_add=True, editable=False)
news_body = models.TextField(max_length=1500)
news_img = models.ImageField(upload_to="pictures/%Y/%m/")
news_author = models.ManyToManyField(Author)
class Meta:
ordering: ['news_id']
def __str__(self):
return '%s %s %s'%(self.news_id, self.news_title, self.news_date)
2条答案
按热度按时间hkmswyz61#
你应该将Django ImageField改为FileField,并将.svg扩展名添加到字段验证器。
文件扩展名验证程序
结果模型文件字段如下所示:
rbl8hiat2#
创建自己的字段,如下所示:
参考:https://gist.github.com/ambivalentno/9bc42b9a417677d96a21