Ratkaistu: kuinka tallentaa svgs django-kuvakenttään SVGAndImageFormFieldillä

Suurin ongelma on, että oletusarvoinen Django-kuvakenttä ei tue svg-tiedostoja. Sinun on käytettävä mukautettua kenttäluokkaa svgs-tiedostojen tallentamiseen.

?

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>

Tämä on mukautettu ImageField, jonka avulla voit ladata joko kuvan tai SVG:n.
To_python-menetelmä ottaa tiedot lomakkeesta ja muuntaa ne FileObjectiksi.
Jos tiedot ovat jo FileObject, se vain palauttaa sen.
Jos tiedoilla on read-attribuutti (eli jos se on tiedostomainen objekti), se palauttaa sen.
Jos tiedot ovat tyhjiä (”), se palauttaa ”(tämä johtuu siitä, että tyhjät tiedostojen latauskentät palauttavat Ei mitään ”, kuten muutkin kentät).
Jos tiedot eivät ole tyhjiä, se yrittää avata sen tiedostona ("rb"-tilassa). Jos tämä onnistuu, se palauttaa tiedostoobjektin. Jos ei, se vain palauttaa tiedot itse (tämä on siksi, että lomakkeet, jotka saattavat odottaa näitä tietoja myöhemmin, eivät katkea).

Salli SVG-tiedostojen lataaminen ImageFieldiin Django adminin kautta

Jos haluat sallia SVG-tiedostojen lataamisen Djangon ImageFieldiin, voit tehdä sen lisäämällä seuraavan rivin settings.py-tiedostoosi:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Kuinka tallentaa Svg-tiedostoja Django-kuvakenttään

On olemassa muutamia eri tapoja tallentaa svg-tiedostoja Djangoon. Yksinkertaisin tapa on käyttää ImageField-luokkaa. Tämän luokan avulla voit määrittää URL-osoitteen tai tiedostopolun kentän arvoksi.

Toinen tapa tallentaa svg-tiedostoja Djangoon on käyttää ImageGallery-luokkaa. Tämän luokan avulla voit määrittää URL-osoitteen tai tiedostopolun kentän arvoksi, ja se luo automaattisesti kuvagallerian polun perusteella.

Related viestiä:

Jätä kommentti