Çözüldü: svg'lerin SVGAndImageFormField ile django görüntü alanında nasıl saklanacağı

Asıl sorun, svg'lerin varsayılan Django görüntü alanı tarafından desteklenmemesidir. Svg'leri depolamak için özel bir alan sınıfı kullanmanız gerekir.

?

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>

Bu, bir resim veya SVG yüklemenizi sağlayan özel bir ImageField'dir.
to_python yöntemi, verileri formdan alır ve bir FileObject'e dönüştürür.
Veri zaten bir FileObject ise, sadece onu döndürür.
Verinin bir okuma özniteliği varsa (yani, dosya benzeri bir nesneyse), onu döndürür.
Veri boşsa (”), ” döndürür (çünkü boş dosya yükleme alanları, diğer alanlar gibi ” yerine Yok döndürür).
Veri boş değilse, onu bir dosya olarak açmaya çalışır ('rb' kipini kullanarak). Bu başarılı olursa, dosya nesnesini döndürür. Değilse, sadece verinin kendisini döndürür (bu, daha sonra bu veriyi bekleyen formların bozulmaması içindir).

SVG dosyalarının Django admin aracılığıyla ImageField'e yüklenmesine izin ver

SVG dosyalarının Django'da bir ImageField'e yüklenmesine izin vermek istiyorsanız, bunu settings.py dosyanıza aşağıdaki satırı ekleyerek yapabilirsiniz:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Django Görüntü Alanında Svgs Nasıl Saklanır

Svg'leri Django'da depolamanın birkaç farklı yolu vardır. En basit yol, ImageField sınıfını kullanmaktır. Bu sınıf, alan değeri olarak bir URL veya dosya yolu belirtmenize olanak tanır.

Svg'leri Django'da depolamanın başka bir yolu da ImageGallery sınıfını kullanmaktır. Bu sınıf, alanın değeri olarak bir URL veya dosya yolu belirtmenize izin verir ve bu yola dayalı olarak otomatik olarak bir resim galerisi oluşturur.

İlgili Mesajlar:

Leave a Comment