Atrisināts: kā saglabāt svgs django attēla laukā, izmantojot SVGAndImageFormField

Galvenā problēma ir tā, ka noklusējuma Django attēla lauks neatbalsta svgs. Lai saglabātu svgs, ir jāizmanto pielāgota lauka klase.

?

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>

Šis ir pielāgots ImageField, kas ļauj augšupielādēt attēlu vai SVG.
Metode to_python ņem datus no veidlapas un pārvērš tos FileObject.
Ja dati jau ir FileObject, tie tos vienkārši atgriež.
Ja datiem ir lasīšanas atribūts (ti, ja tas ir failam līdzīgs objekts), tie to atgriež.
Ja dati ir tukši (”), tie atgriež ” (tas ir tāpēc, ka tukši failu augšupielādes lauki atgriež Nav, nevis ”, tāpat kā citi lauki).
Ja dati nav tukši, tas mēģina tos atvērt kā failu (izmantojot “rb” režīmu). Ja tas izdodas, tas atgriež faila objektu. Ja nē, tas vienkārši atgriež pašus datus (tas ir tāpēc, lai veidlapas, kuras vēlāk varētu sagaidīt šos datus, netiktu sabojātas).

Ļauj augšupielādēt SVG failus ImageField, izmantojot Django admin

Ja vēlaties atļaut SVG failu augšupielādi ImageField programmā Django, varat to izdarīt, failam settings.py pievienojot šādu rindiņu:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Kā saglabāt Svgs Django attēla laukā

Ir daži dažādi veidi, kā Django saglabāt svgs. Vienkāršākais veids ir izmantot ImageField klasi. Šī klase ļauj norādīt URL vai faila ceļu kā lauka vērtību.

Vēl viens veids, kā Django saglabāt svgs, ir izmantot ImageGallery klasi. Šī klase ļauj norādīt URL vai faila ceļu kā lauka vērtību, un tā automātiski ģenerēs attēlu galeriju, pamatojoties uz šo ceļu.

Related posts:

Leave a Comment