Yakagadziriswa: maitiro ekuchengeta svgs mu django mufananidzo munda neSVGA uyeImageFormField

Dambudziko guru nderekuti svgs haitsigirwe neiyo default Django mufananidzo ndima. Iwe unofanirwa kushandisa tsika yemunda kirasi kuchengetedza 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>

Iyi itsika ImageField inobvumidza iwe kurodha chero mufananidzo kana SVG.
Iyo to_python nzira inotora iyo data kubva pafomu uye inoshandura kuita FileObject.
Kana iyo data yatova FileObject, inongoidzorera.
Kana iyo data iine hunhu hwekuverenga (kureva kana iri faira-yakafanana chinhu), inoidzosera.
Kana iyo data isina chinhu (”), inodzoka ” (izvi zvinodaro nekuti isina faira yekurodha minda inodzoka Hapana pane ”, semamwe minda).
Kana iyo data isina chinhu, inoedza kuivhura sefaira (ichishandisa iyo 'rb' modhi). Kana izvi zvikabudirira, zvinodzorera chinhu chefaira. Kana zvisiri, inongodzosera iyo data pachayo (izvi kuitira kuti mafomu angave achitarisira iyi data gare gare asatyoke).

Bvumira mafaera eSVG kuti aiswe kuImageField kuburikidza neDjango admin

Kana iwe uchida kubvumira mafaera eSVG kuti aiswe kuImageField muDjango, unogona kuzviita nekuwedzera mutsara unotevera kune yako settings.py faira:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Maitiro Ekuchengetedza Svgs MuDjango Image Field

Pane nzira shoma dzakasiyana dzekuchengeta svgs muDjango. Nzira iri nyore ndeye kushandisa iyo ImageField kirasi. Iyi kirasi inokubvumira kuti utaure URL kana filepath sekukosha kwemunda.

Imwe nzira yekuchengetedza svgs muDjango ndeye kushandisa iyo ImageGallery kirasi. Iri kirasi rinokutendera kuti utaure URL kana filepath sekukosha kwemunda, uye inozogadzira yega gallery yemifananidzo yakavakirwa munzira iyoyo.

Related posts:

Leave a Comment