Glavni problem je što svgs nije podržan od strane podrazumevanog polja Django slike. Morate koristiti prilagođenu klasu polja za pohranjivanje svgs-a.
? 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>
Ovo je prilagođeno ImageField koje vam omogućava da otpremite ili sliku ili SVG.
Metoda to_python uzima podatke iz obrasca i pretvara ih u FileObject.
Ako su podaci već FileObject, samo ih vraćaju.
Ako podaci imaju atribut čitanja (tj. ako je objekt sličan fajlu), vraćaju ga.
Ako su podaci prazni (”), vraćaju se ” (to je zato što prazna polja za otpremanje datoteke vraćaju Ništa umjesto “”, kao i druga polja).
Ako podaci nisu prazni, pokušava ih otvoriti kao datoteku (koristeći 'rb' mod). Ako ovo uspije, vraća se objekt datoteke. Ako ne, samo vraća podatke (ovo je tako da se obrasci koji bi kasnije mogli očekivati ove podatke ne bi pokvarili).
Dozvolite da se SVG fajlovi otpreme na ImageField preko Django administratora
Ako želite da dozvolite da se SVG fajlovi otpremaju na ImageField u Djangu, to možete učiniti dodavanjem sledećeg reda u datoteku settings.py:
IMAGE_FIELD_MAX_FILE_SIZE = 1000000
Kako pohraniti Svgs u polje Django slike
Postoji nekoliko različitih načina za čuvanje svgs-a u Djangu. Najjednostavniji način je korištenje klase ImageField. Ova klasa vam omogućava da navedete URL ili putanju datoteke kao vrijednost za polje.
Drugi način za pohranjivanje svgs-a u Django je korištenje klase ImageGallery. Ova klasa vam omogućava da odredite URL ili putanju datoteke kao vrijednost za polje i automatski će generirati galeriju slika na osnovu te putanje.