如何将SVG文件上传到Django应用?

时间:2019-09-18 15:57:54

标签: html django python-3.x

我尝试通过管理站点将.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)

1 个答案:

答案 0 :(得分:0)

您已将Django ImageField更改为FileField,并将.svg扩展名添加到字段验证器。

File Extension Validator

结果模型文件字段如下:

news_img = models.FileField(upload_to="pictures/%Y/%m/", validators=[ExtensionValidator(['pdf', 'doc', 'svg'])])