Lahendatud: kuidas salvestada SVGA-d django pildiväljale SVGAndImageFormFieldiga

Peamine probleem seisneb selles, et Django vaikepildiväli ei toeta svg-sid. Svg-ide salvestamiseks peate kasutama kohandatud väljaklassi.

?

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>

See on kohandatud pildiväli, mis võimaldab teil üles laadida kas pildi või SVG-vormingus.
Meetod to_python võtab vormilt andmed ja teisendab need failiobjektiks.
Kui andmed on juba FileObject, tagastab see need lihtsalt.
Kui andmetel on lugemisatribuut (st kui tegemist on failitaolise objektiga), tagastab see selle.
Kui andmed on tühjad (”), tagastab see "(seda põhjusel, et tühjad faili üleslaadimise väljad tagastavad "" asemel "" nagu ka teised väljad).
Kui andmed pole tühjad, proovib see neid failina avada (kasutades rb-režiimi). Kui see õnnestub, tagastab see failiobjekti. Kui ei, tagastab see lihtsalt andmed ise (see on selleks, et vormid, mis võivad neid andmeid hiljem oodata, ei puruneks).

Lubage Django administraatori kaudu SVG-faile ImageFieldi üles laadida

Kui soovite lubada SVG-failide üleslaadimist Django ImageFieldi, saate seda teha, lisades oma settings.py failile järgmise rea:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Kuidas salvestada SVG-sid Django pildiväljale

Svg-de salvestamiseks Djangos on mitu erinevat viisi. Lihtsaim viis on kasutada ImageFieldi klassi. See klass võimaldab teil määrata välja väärtusena URL-i või failitee.

Teine võimalus SVG-de salvestamiseks Djangos on kasutada ImageGallery klassi. See klass võimaldab teil määrata välja väärtusena URL-i või failitee ja see loob selle tee põhjal automaatselt piltide galerii.

Seonduvad postitused:

Jäta kommentaar