Nalutas: kung paano mag-imbak ng mga svg sa django image field na may SVGAndImageFormField

Ang pangunahing problema ay ang mga svg ay hindi suportado ng default na field ng imahe ng Django. Kailangan mong gumamit ng custom na field class para mag-imbak ng mga 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>

Ito ay isang custom na ImageField na nagbibigay-daan sa iyong mag-upload ng alinman sa isang imahe o isang SVG.
Kinukuha ng paraan ng to_python ang data mula sa form at kino-convert ito sa isang FileObject.
Kung ang data ay isa nang FileObject, ibinabalik lamang nito.
Kung ang data ay may read attribute (ibig sabihin, kung ito ay isang file-like object), ibinabalik nito ito.
Kung ang data ay walang laman (”), ito ay nagbabalik ng ” (ito ay dahil ang walang laman na mga field sa pag-upload ng file ay nagbabalik ng Wala sa halip na ”, tulad ng ibang mga field).
Kung walang laman ang data, susubukan nitong buksan ito bilang isang file (gamit ang 'rb' mode). Kung magtagumpay ito, ibabalik nito ang file object. Kung hindi, ibinabalik lang nito ang mismong data (ito ay para hindi masira ang mga form na maaaring umaasa sa data na ito sa bandang huli).

Payagan ang mga SVG na file na ma-upload sa ImageField sa pamamagitan ng Django admin

Kung gusto mong payagan ang mga SVG na file na ma-upload sa isang ImageField sa Django, magagawa mo ito sa pamamagitan ng pagdaragdag ng sumusunod na linya sa iyong settings.py file:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Paano Mag-imbak ng mga Svg Sa Django Image Field

Mayroong ilang iba't ibang paraan upang mag-imbak ng mga svg sa Django. Ang pinakasimpleng paraan ay ang paggamit ng klase ng ImageField. Binibigyang-daan ka ng klase na ito na tumukoy ng URL o filepath bilang halaga para sa field.

Ang isa pang paraan upang mag-imbak ng mga svg sa Django ay ang paggamit ng klase ng ImageGallery. Binibigyang-daan ka ng klase na ito na tumukoy ng URL o filepath bilang halaga para sa field, at awtomatiko itong bubuo ng gallery ng mga larawan batay sa path na iyon.

Kaugnay na mga post:

Mag-iwan ng komento