تم الحل: كيفية تخزين svgs في حقل صورة django باستخدام SVGAndImageFormField

المشكلة الرئيسية هي أن svgs لا يدعمها حقل صورة Django الافتراضي. تحتاج إلى استخدام فئة حقل مخصصة لتخزين svgs.

?

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>

هذا ImageField مخصص يسمح لك بتحميل صورة أو SVG.
تأخذ طريقة to_python البيانات من النموذج وتحولها إلى FileObject.
إذا كانت البيانات عبارة عن FileObject بالفعل ، فإنها تقوم بإعادتها فقط.
إذا كانت البيانات تحتوي على سمة قراءة (أي إذا كانت كائنًا يشبه الملف) ، فإنها تُعيدها.
إذا كانت البيانات فارغة (") ، فإنها تُرجع" (هذا لأن حقول تحميل الملف الفارغة تُرجع بلا بدلاً من "، مثل الحقول الأخرى).
إذا لم تكن البيانات فارغة ، فسيحاول فتحها كملف (باستخدام الوضع "rb"). في حالة نجاح ذلك ، تقوم بإرجاع كائن الملف. إذا لم يكن الأمر كذلك ، فإنه يقوم فقط بإرجاع البيانات نفسها (وهذا حتى لا تنكسر النماذج التي قد تتوقع هذه البيانات لاحقًا).

السماح بتحميل ملفات SVG إلى ImageField عبر مشرف Django

إذا كنت تريد السماح بتحميل ملفات SVG إلى ImageField في Django ، فيمكنك القيام بذلك عن طريق إضافة السطر التالي إلى ملف settings.py:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

كيفية تخزين Svgs في حقل صورة Django

هناك عدة طرق مختلفة لتخزين svgs في Django. إن أبسط طريقة هي استخدام فئة ImageField. تسمح لك هذه الفئة بتحديد عنوان URL أو مسار الملف كقيمة للحقل.

هناك طريقة أخرى لتخزين svgs في Django وهي استخدام فئة ImageGallery. تسمح لك هذه الفئة بتحديد عنوان URL أو مسار الملف كقيمة للحقل ، وستقوم تلقائيًا بإنشاء معرض للصور بناءً على هذا المسار.

الوظائف ذات الصلة:

اترك تعليق