Išspręsta: kaip išsaugoti svgs django vaizdo lauke naudojant SVGAndImageFormField

Pagrindinė problema yra ta, kad numatytasis Django vaizdo laukas nepalaiko svgs. Norėdami saugoti SVGS, turite naudoti pasirinktinę lauko klasę.

?

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>

Tai tinkintas vaizdo laukas, leidžiantis įkelti vaizdą arba SVG.
Metodas to_python paima duomenis iš formos ir konvertuoja juos į FileObject.
Jei duomenys jau yra „FileObject“, jie tiesiog juos grąžina.
Jei duomenys turi skaitymo atributą (ty jei tai į failą panašus objektas), jie jį grąžina.
Jei duomenys yra tušti (”), jie grąžina “ (taip yra todėl, kad tušti failo įkėlimo laukai pateikia Nėra, o ne “, kaip ir kiti laukai).
Jei duomenys nėra tušti, jis bando atidaryti juos kaip failą (naudodamas „rb“ režimą). Jei tai pavyksta, jis grąžina failo objektą. Jei ne, jis tiesiog grąžina pačius duomenis (tai daroma taip, kad formos, kurios vėliau gali tikėtis šių duomenų, nenutrūktų).

Leisti SVG failus įkelti į ImageField per Django admin

Jei norite leisti SVG failus įkelti į „ImageField“ programoje „Django“, tai galite padaryti pridėdami šią eilutę prie failo settings.py:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Kaip saugoti SVG Django vaizdo lauke

Yra keletas skirtingų būdų, kaip „Django“ saugoti SVG. Paprasčiausias būdas yra naudoti ImageField klasę. Ši klasė leidžia nurodyti URL arba failo kelią kaip lauko reikšmę.

Kitas būdas saugoti svgs „Django“ yra naudoti „ImageGallery“ klasę. Ši klasė leidžia nurodyti URL arba failo kelią kaip lauko reikšmę ir pagal tą kelią automatiškai sugeneruos vaizdų galeriją.

Susijusios naujienos:

Palikite komentarą