Isonjululwe: uzigcina njani ii-svgs kwintsimi yomfanekiso we-django nge-SVGAndImageFormField

Ingxaki ephambili kukuba ii-svgs azixhaswanga ngumhlaba womfanekiso we-Django ongagqibekanga. Kufuneka usebenzise iklasi yendawo yokugcina iisvgs.

?

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>

Lo nguMmandla woMfanekiso wesiko okuvumela ukuba ulayishe nokuba ngumfanekiso okanye iSVG.
Indlela ye-to_python ithatha idatha kwifom kwaye iyiguqule ibe yiFayileObject.
Ukuba idatha sele iyiFayileObject, ibuyisela nje.
Ukuba idatha inophawu lokufunda (oko kukuthi ukuba yinto efana nefayile), iyayibuyisela.
Ukuba idatha ayinanto (”), ibuyisela ” (oku kungenxa yokuba iindawo ezingenanto zokulayisha iifayile zibuyisela Akukho nanye endaweni ye”, njengamanye amasimi).
Ukuba idatha ayinalutho, izama ukuyivula njengefayile (isebenzisa imo ye 'rb'). Ukuba oku kuphumelela, ibuyisela into yefayile. Ukuba akunjalo, ibuyisela idatha ngokwayo (oku kwenzelwa ukuba iifom ezinokuthi zilindele le datha kamva zingaphuli).

Vumela iifayile ze-SVG ukuba zifakwe kwi-ImageField nge-Django admin

Ukuba ufuna ukuvumela iifayile ze-SVG ukuba zifakwe kwi-ImageField kwi-Django, ungakwenza oko ngokongeza lo mgca ulandelayo kwifayile yakho ye-setting.py:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Uzigcina njani iiSvgs kwiNdawo yoMfanekiso weDjango

Kukho iindlela ezimbalwa ezahlukeneyo zokugcina ii-svgs kwi-Django. Eyona ndlela ilula kukusebenzisa iklasi ye-ImageField. Olu didi lukuvumela ukuba uchaze i-URL okanye indlela yefayile njengexabiso lentsimi.

Enye indlela yokugcina ii-svgs kwi-Django kukusebenzisa iklasi ye-ImageGallery. Olu didi lukuvumela ukuba uchaze i-URL okanye indlela yefayile njengexabiso lentsimi, kwaye iya kuvelisa ngokuzenzekelayo igalari yemifanekiso esekelwe kuloo ndlela.

Izithuba ezihambelanayo:

Shiya Comment