निराकरण: 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>

हे एक सानुकूल इमेजफील्ड आहे जे तुम्हाला इमेज किंवा SVG अपलोड करण्याची परवानगी देते.
to_python पद्धत फॉर्ममधून डेटा घेते आणि त्यास FileObject मध्ये रूपांतरित करते.
जर डेटा आधीच फाइलऑब्जेक्ट असेल तर तो फक्त तो परत करतो.
जर डेटामध्ये रीड अॅट्रिब्यूट असेल (म्हणजे जर ती फाईल सारखी ऑब्जेक्ट असेल तर), ते ते परत करते.
जर डेटा रिकामा (”), तो परत येतो” (याचे कारण असे आहे की रिक्त फाइल अपलोड फील्ड इतर फील्ड प्रमाणे” ऐवजी काहीही देत ​​नाहीत).
डेटा रिकामा नसल्यास, तो फाइल म्हणून उघडण्याचा प्रयत्न करतो ('rb' मोड वापरून). हे यशस्वी झाल्यास, ते फाइल ऑब्जेक्ट परत करते. नसल्यास, ते फक्त डेटा स्वतःच परत करते (हे असे आहे की या डेटाची अपेक्षा असलेले फॉर्म नंतर खंडित होणार नाहीत).

SVG फायली Django प्रशासकाद्वारे ImageField वर अपलोड करण्याची अनुमती द्या

तुम्हाला SVG फाइल्स Django मधील ImageField वर अपलोड करण्याची परवानगी द्यायची असल्यास, तुम्ही तुमच्या settings.py फाइलमध्ये खालील ओळ जोडून असे करू शकता:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

जॅंगो इमेज फील्डमध्ये Svgs कसे संग्रहित करावे

Django मध्ये svgs संचयित करण्याचे काही वेगळे मार्ग आहेत. इमेजफील्ड क्लास वापरणे हा सर्वात सोपा मार्ग आहे. हा वर्ग तुम्हाला फील्डसाठी मूल्य म्हणून URL किंवा फाइलपाथ निर्दिष्ट करण्याची परवानगी देतो.

Django मध्ये svgs संचयित करण्याचा दुसरा मार्ग म्हणजे ImageGallery वर्ग वापरणे. हा वर्ग तुम्हाला फील्डसाठी मूल्य म्हणून URL किंवा फाइलपाथ निर्दिष्ट करण्याची परवानगी देतो आणि ते त्या मार्गावर आधारित प्रतिमांची गॅलरी स्वयंचलितपणे तयार करेल.

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

एक टिप्पणी द्या