已解決:如何使用 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 或文件路徑作為字段的值,它會自動生成基於該路徑的圖像庫。

相關文章:

發表評論