हल: SVGAndImageFormField के साथ django छवि क्षेत्र में svgs को कैसे स्टोर करें

मुख्य समस्या यह है कि 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>

यह एक कस्टम इमेजफिल्ड है जो आपको एक छवि या एक एसवीजी अपलोड करने की अनुमति देता है।
To_python विधि डेटा को प्रपत्र से लेती है और इसे FileObject में परिवर्तित करती है।
यदि डेटा पहले से ही एक फ़ाइलऑब्जेक्ट है, तो यह इसे वापस कर देता है।
यदि डेटा में एक पठन विशेषता है (यानी यदि यह फ़ाइल जैसी वस्तु है), तो यह इसे वापस कर देता है।
यदि डेटा खाली है ("), तो यह " वापस आ जाता है (ऐसा इसलिए है क्योंकि खाली फ़ाइल अपलोड फ़ील्ड अन्य फ़ील्ड्स की तरह " के बजाय कोई नहीं लौटाते हैं)।
यदि डेटा खाली नहीं है, तो यह इसे फ़ाइल के रूप में खोलने का प्रयास करता है ('आरबी' मोड का उपयोग करके)। यदि यह सफल होता है, तो यह फ़ाइल ऑब्जेक्ट लौटाता है। यदि नहीं, तो यह केवल डेटा को ही लौटाता है (ऐसा इसलिए है ताकि बाद में इस डेटा की अपेक्षा करने वाले प्रपत्र टूट न जाएं)।

Django व्यवस्थापक के माध्यम से SVG फ़ाइलों को ImageField पर अपलोड करने की अनुमति दें

यदि आप एसवीजी फ़ाइलों को Django में एक छविफिल्ड पर अपलोड करने की अनुमति देना चाहते हैं, तो आप निम्न पंक्ति को अपनी settings.py फ़ाइल में जोड़कर ऐसा कर सकते हैं:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

कैसे Django छवि क्षेत्र में Svgs स्टोर करने के लिए

Django में svgs को स्टोर करने के कुछ अलग तरीके हैं। ImageField क्लास का उपयोग करने का सबसे आसान तरीका है। यह वर्ग आपको फ़ील्ड के मान के रूप में URL या फ़ाइलपथ निर्दिष्ट करने की अनुमति देता है।

Django में svgs को स्टोर करने का दूसरा तरीका ImageGallery क्लास का उपयोग करना है। यह वर्ग आपको फ़ील्ड के मान के रूप में एक URL या फ़ाइलपथ निर्दिष्ट करने की अनुमति देता है, और यह स्वचालित रूप से उस पथ के आधार पर छवियों की एक गैलरी उत्पन्न करेगा।

संबंधित पोस्ट:

एक टिप्पणी छोड़ दो