【问题标题】:How can I upload svg file to a django app?如何将 svg 文件上传到 django 应用程序?
【发布时间】:2020-01-19 15:28:15
【问题描述】:

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

【问题讨论】:

标签: html django python-3.x


【解决方案1】:

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

File Extension Validator

结果模型文件字段是这样的:

from django.core.validators import FileExtensionValidator

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

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2015-10-03
    • 2016-10-26
    相关资源
    最近更新 更多