8

I tried to upload an .svg by the admin site to SQLite (django's default db) and I got the following error:

Upload a valid image. The file you uploaded was either not an image or a corrupted image.

I can upload .jpg files and it works properly.

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)
VicenteC
  • 323
  • 5
  • 23
  • 1
    Does this answer your question? [Allow SVG files to be uploaded to ImageField via Django admin](https://stackoverflow.com/questions/38006200/allow-svg-files-to-be-uploaded-to-imagefield-via-django-admin) – mrts Oct 04 '20 at 06:22

1 Answers1

14

You sould change your Django ImageField to FileField and add .svg extension to field validators.

File Extension Validator

The result model file field is something like this:

from django.core.validators import FileExtensionValidator

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