已解决:如何使用 SVGAndImageFormField 在 django 图像字段中存储 svg

主要问题是默认的 Django 图像字段不支持 svgs。 您需要使用自定义字段类来存储 svg。

?

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>

这是一个自定义 ImageField,允许您上传图像或 SVG。
to_python 方法从表单中获取数据并将其转换为 FileObject。
如果数据已经是一个 FileObject,它就返回它。
如果数据具有读取属性(即,如果它是类文件对象),则返回它。
如果数据为空(”),则返回”(这是因为空文件上传字段返回 None 而不是像其他字段一样返回“”)。
如果数据不为空,它会尝试将其作为文件打开(使用“rb”模式)。 如果成功,它返回文件对象。 如果不是,它只返回数据本身(这样以后可能需要此数据的表单就不会中断)。

允许通过 Django 管理将 SVG 文件上传到 ImageField

如果要允许将 SVG 文件上传到 Django 中的 ImageField,可以通过将以下行添加到 settings.py 文件来实现:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

如何在 Django 图像字段中存储 Svg

有几种不同的方法可以在 Django 中存储 svg。 最简单的方法是使用 ImageField 类。 此类允许您指定 URL 或文件路径作为字段的值。

在 Django 中存储 svg 的另一种方法是使用 ImageGallery 类。 此类允许您指定 URL 或文件路径作为字段的值,它会自动生成基于该路径的图像库。

相关文章:

发表评论