Kuxazululiwe: uwagcina kanjani ama-svgs kunkambu yesithombe se-django nge-SVGAndImageFormField

Inkinga enkulu ukuthi ama-svgs awasekelwa inkambu yesithombe ye-Django ezenzakalelayo. Udinga ukusebenzisa isigaba senkambu yangokwezifiso ukuze ugcine ama-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>

Lena i-ImageField yangokwezifiso ekuvumela ukuthi ulayishe isithombe noma i-SVG.
Indlela ye-to_python ithatha idatha efomini bese iyiguqulela ku-FileObject.
Uma idatha isivele iyi-FileObject, ivele iyibuyisele.
Uma idatha inesibaluli esifundwayo (okungukuthi uma iyinto efana nefayela), iyayibuyisela.
Uma idatha ingenalutho (”), ibuyisela ” (lokhu kungenxa yokuthi izinkambu zokulayisha ifayela ezingenalutho zibuyisela Ayikho esikhundleni sokuthi ”, njengezinye izinkambu).
Uma idatha ingenalutho, izama ukuyivula njengefayela (isebenzisa imodi ye-'rb'). Uma lokhu kuphumelela, kubuyisela into yefayela. Uma kungenjalo, ivele ibuyisele idatha ngokwayo (lokhu kwenzelwa ukuthi amafomu okungenzeka alindele le datha kamuva angaphuli).

Vumela amafayela e-SVG ukuthi alayishwe ku-ImageField ngomlawuli we-Django

Uma ufuna ukuvumela amafayela e-SVG ukuthi alayishwe ku-ImageField ku-Django, ungakwenza lokho ngokwengeza umugqa olandelayo kufayela lakho le-setting.py:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Ungawagcina Kanjani Ama-Svgs Kunkambu Yesithombe se-Django

Kunezindlela ezimbalwa ezihlukile zokugcina ama-svgs e-Django. Indlela elula ukusebenzisa isigaba se-ImageField. Lesi sigaba sikuvumela ukuthi ucacise i-URL noma indlela yefayela njengevelu lenkambu.

Enye indlela yokugcina ama-svgs ku-Django ukusebenzisa isigaba se-ImageGallery. Leli klasi likuvumela ukuthi ucacise i-URL noma indlela yefayela njengevelu lenkambu, futhi lizokhiqiza ngokuzenzakalelayo igalari yezithombe ngokusekelwe kuleyo ndlela.

Okuthunyelwe okuhlobene:

Shiya amazwana