Hlavným problémom je, že štandardné pole obrázka Django nepodporuje svgs. Na ukladanie svgs musíte použiť vlastnú triedu poľ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>
Toto je vlastné ImageField, ktoré vám umožňuje nahrať buď obrázok alebo SVG.
Metóda to_python preberá údaje z formulára a konvertuje ich na FileObject.
Ak sú údaje už objektom FileObject, vráti ich.
Ak majú dáta atribút read (tj ak ide o objekt podobný súboru), vráti ho.
Ak sú údaje prázdne (”), vráti sa ” (je to preto, že prázdne polia nahrávania súboru vrátia Žiadne namiesto ”, ako ostatné polia).
Ak údaje nie sú prázdne, pokúsi sa ich otvoriť ako súbor (pomocou režimu „rb“). Ak sa to podarí, vráti objekt súboru. Ak nie, vráti iba samotné údaje (je to preto, aby sa formuláre, ktoré môžu tieto údaje neskôr očakávať, nepokazili).
Povoliť nahrávanie súborov SVG do ImageField prostredníctvom správcu Django
Ak chcete povoliť nahrávanie súborov SVG do poľa ImageField v Django, môžete tak urobiť pridaním nasledujúceho riadku do súboru settings.py:
IMAGE_FIELD_MAX_FILE_SIZE = 1000000 XNUMX XNUMX
Ako ukladať Svgs v poli obrázka Django
Existuje niekoľko rôznych spôsobov, ako uložiť svgs v Django. Najjednoduchším spôsobom je použiť triedu ImageField. Táto trieda vám umožňuje zadať adresu URL alebo cestu k súboru ako hodnotu poľa.
Ďalším spôsobom, ako uložiť svgs v Django, je použiť triedu ImageGallery. Táto trieda vám umožňuje zadať adresu URL alebo cestu k súboru ako hodnotu pre pole a na základe tejto cesty automaticky vygeneruje galériu obrázkov.