נפתרה: כיצד לאחסן 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 בשדה תמונה של ג'נגו

ישנן כמה דרכים שונות לאחסן svgs ב-Django. הדרך הפשוטה ביותר היא להשתמש במחלקה ImageField. מחלקה זו מאפשרת לך לציין כתובת URL או נתיב קובץ כערך עבור השדה.

דרך נוספת לאחסן svgs ב-Django היא להשתמש במחלקה ImageGallery. מחלקה זו מאפשרת לך לציין כתובת URL או נתיב קובץ כערך עבור השדה, והיא תיצור אוטומטית גלריית תמונות המבוססת על נתיב זה.

הודעות קשורות:

השאירו תגובה