Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow SVG files to be uploaded to ImageField via Django admin

I'm switching to SVG images to represent categories on my e-commerce platform. I was using models.ImageField in the Category model to store the images before, but the forms.ImageField validation is not capable of handling a vector-based image (and therefore rejects it).

I don't require thorough validation against harmful files, since all uploads will be done via the Django Admin. It looks like I'll have to switch to a models.FileField in my model, but I do want warnings against uploading invalid images.

Nick Khlestov wrote a SVGAndImageFormField (find source within the article, I don't have enough reputation to post more links) over django-rest-framework's ImageField. How do I use this solution over Django's ImageField (and not the DRF one)?

like image 492
Yash Tewari Avatar asked Jun 24 '16 05:06

Yash Tewari


People also ask

What is the use of image field in Django?

The image column is an ImageField field that works with the Django’s file storage API, which provides a way to store and retrieve files, as well as read and write them. The upload_to parameters specify the location where images will be stored which for this model is MEDIA_ROOT/images/

Is it possible to store SVG files in an imagefield?

Looking to see if anyone has come up with a creative approach for storing SVG files in an ImageField. Out of the box django doesn't support storing of SVG files in an ImageField.

How to upload images in Django 1?

Uploading Images in Django 1 settings.py. MEDIA_URL is the URL that will serve the media files and MEDIA_ROOT is the path to the root directory where the files are getting stored. 2 urls.py. With that Django’s development server is capable of serving media files. ... 3 forms.py. ... 4 index.html. ... 5 views.py. ... 6 urls.py. ...

How to create a form from a filefield in Django?

The upload_to attribute in the FileField points to where the files will be stored in the application Migrations will create actual tables in the database. Django comes with an inbuilt ModelForm class, making it easy to create forms from model fields. Create a new file called forms.py and add the following code


1 Answers

I have never used SVGAndImageFormField so I cannot really comment on that. Personally I would have opted for a simple application of FileField, but that clearly depends on the project requirements. I will expand on that below:

As mentioned in the comment, the basic difference between an ImageField and a FileField is that the first checks if a file is an image using Pillow:

Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.

Reference: Django docs, Django source code

It also offers a couple of attributes possibly irrelevant to the SVG case (height, width).

Therefore, the model field could be:

    svg = models.FileField(upload_to=..., validators=[validate_svg])

You can use a function like is_svg as provided in the relevant question:

How can I say a file is SVG without using a magic number?

Then a function to validate SVG:

def validate_svg(file, valid):
    if not is_svg(file):
        raise ValidationError("File not svg")
like image 95
Wtower Avatar answered Oct 14 '22 09:10

Wtower