தீர்க்கப்பட்டது: SVGAndImageFormField உடன் django பட புலத்தில் svgs ஐ எவ்வாறு சேமிப்பது

முக்கிய பிரச்சனை என்னவென்றால், இயல்புநிலை ஜாங்கோ பட புலத்தால் svgs ஆதரிக்கப்படவில்லை. 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 இல் பதிவேற்ற அனுமதிக்கவும்

ஜாங்கோவில் உள்ள இமேஜ்ஃபீல்டில் SVG கோப்புகளைப் பதிவேற்ற அனுமதிக்க விரும்பினால், உங்கள் settings.py கோப்பில் பின்வரும் வரியைச் சேர்ப்பதன் மூலம் அவ்வாறு செய்யலாம்:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

ஜாங்கோ படத் துறையில் Svgs ஐ எவ்வாறு சேமிப்பது

ஜாங்கோவில் svgs ஐ சேமிக்க சில வெவ்வேறு வழிகள் உள்ளன. இமேஜ்ஃபீல்ட் வகுப்பைப் பயன்படுத்துவது எளிமையான வழி. புலத்திற்கான மதிப்பாக URL அல்லது கோப்புப் பாதையைக் குறிப்பிட இந்த வகுப்பு உங்களை அனுமதிக்கிறது.

ஜாங்கோவில் svgs ஐ சேமிப்பதற்கான மற்றொரு வழி ImageGallery வகுப்பைப் பயன்படுத்துவதாகும். புலத்திற்கான மதிப்பாக URL அல்லது கோப்புப் பாதையைக் குறிப்பிட இந்த வகுப்பு உங்களை அனுமதிக்கிறது, மேலும் அந்தப் பாதையின் அடிப்படையில் அது தானாகவே படங்களின் கேலரியை உருவாக்கும்.

தொடர்புடைய இடுகைகள்:

ஒரு கருத்துரையை