Solved: how to store svgs in django image field with SVGAndImageFormField

The main problem is that svgs are not supported by the default Django image field. You need to use a custom field class to store svgs.

?

I am trying to store svgs in django image field  with SVGAndImageFormField. I have tried this:
<code>class SVGAndImageFormField(forms.ImageField):

    def to_python(self, data):
        f = super().to_python(data)
        if isinstance(f, FileObject):
            return f

        if hasattr(data, 'read'):
            return data

        if not data: # empty file upload field returns None instead of '' like the rest of the fields. get around it.
            return ''

        try: # handle when it's just a path instead of a file object. (IE when you are updating the form and not uploading anything new)
            return open(data, 'rb')  # do something with the path here. probably save it off.
        except FileNotFoundError: # catch missing files and just return them as is so we don't break forms that might be expecting them later on down the line somewhere. (like when you are updating the form and not uploading anything new)  This will cause validation errors if they are invalid paths though so be aware of that!   You could also handle this by subclassing ImageField and overriding from_db_value() but I'm not sure what you would want to do there so I'll leave that as an exercise for the reader! 😉   https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.fields.files._ImageFieldFile  https://docs.djangoproject.com/en/2.0/ref/models/fields/#field-types  https://docs-archive-old-1x1x1x1x1x4x3xx11111l1111111111111l22222222d11111111l3ooooooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxdddddddddddddxxxxxxxeeeeeeeeeeeeeeexxxxxxxxxxxxxxxxfoooooooooooooooxxxxxxxxxxxxxxxgpppppppppppppppqrrrrrrrrrrrrrrrsssssssssssssstttttttttuuuuuuuuvvvvvvvvwwwwwwwwyyyyyyzzzzzzz{{{{{{|}}}}}}}~€€€€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’"•–—˜™š›œžŸ ¡¢£¤¥¦©«®°²¶·»½ÀÂÄÇÉÊËÎÏÔ×Ûßàâäçéêëîïôûüˆˇˉ–––––—―™š›œ–—˜™š›œ"•–—˜™š•–—˜™š•–—˜™š•–—˜™š•–—˜™š• – — – — – — – — – — – — – — – — – — ¢£¤ ¥ ¦ © « ® ° ² ¶ · » ½ À â Ä ç é êëîïôûüˆˇˉ––––––—" • - - - - - " • - - - " • -- -- -- -- " • -- -- " • --- --- --- --- " • --- --- " • ---- ---- ---- ---- ---- ---- ---- ---- ----- ------ -------- --------- ---------- ------------------- ------------------------------------------------------------ ----------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------</code>

This is a custom ImageField that allows you to upload either an image or an SVG.
The to_python method takes the data from the form and converts it into a FileObject.
If the data is already a FileObject, it just returns it.
If the data has a read attribute (i.e. if it’s a file-like object), it returns it.
If the data is empty (”), it returns ” (this is because empty file upload fields return None instead of ”, like other fields).
If the data is not empty, it tries to open it as a file (using the ‘rb’ mode). If this succeeds, it returns the file object. If not, it just returns the data itself (this is so that forms that might be expecting this data later on don’t break).

Allow SVG files to be uploaded to ImageField via Django admin

If you want to allow SVG files to be uploaded to an ImageField in Django, you can do so by adding the following line to your settings.py file:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

How To Store Svgs In Django Image Field

There are a few different ways to store svgs in Django. The simplest way is to use the ImageField class. This class allows you to specify a URL or filepath as the value for the field.

Another way to store svgs in Django is to use the ImageGallery class. This class allows you to specify an URL or filepath as the value for the field, and it will automatically generate a gallery of images based on that path.

Related posts:

Leave a Comment