ઉકેલાયેલ: SVGAndImageFormField સાથે ડીજેંગો ઇમેજ ફીલ્ડમાં 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' મોડનો ઉપયોગ કરીને). જો આ સફળ થાય, તો તે ફાઇલ ઑબ્જેક્ટ પરત કરે છે. જો નહિં, તો તે ફક્ત ડેટા જ પરત કરે છે (આ એટલા માટે છે કે જે ફોર્મ્સ પછીથી આ ડેટાની અપેક્ષા રાખતા હોય તે તૂટી ન જાય).

Django એડમિન દ્વારા SVG ફાઇલોને ImageField પર અપલોડ કરવાની મંજૂરી આપો

જો તમે SVG ફાઇલોને Djangoમાં ImageField પર અપલોડ કરવાની મંજૂરી આપવા માંગતા હો, તો તમે તમારી settings.py ફાઇલમાં નીચેની લાઇન ઉમેરીને આમ કરી શકો છો:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

Django ઈમેજ ફીલ્ડમાં Svgs ને કેવી રીતે સ્ટોર કરવું

Django માં svgs સ્ટોર કરવાની કેટલીક અલગ અલગ રીતો છે. સૌથી સરળ રીત ImageField ક્લાસનો ઉપયોગ કરવાનો છે. આ વર્ગ તમને ક્ષેત્ર માટે મૂલ્ય તરીકે URL અથવા ફાઇલપાથનો ઉલ્લેખ કરવાની મંજૂરી આપે છે.

Django માં svgs સ્ટોર કરવાની બીજી રીત છે ImageGallery ક્લાસનો ઉપયોગ કરવો. આ વર્ગ તમને ફીલ્ડ માટે મૂલ્ય તરીકે URL અથવા ફાઇલપાથનો ઉલ્લેખ કરવાની મંજૂરી આપે છે, અને તે આપમેળે તે પાથ પર આધારિત છબીઓની ગેલેરી જનરેટ કરશે.

સંબંધિત પોસ્ટ્સ:

પ્રતિક્રિયા આપો