ಪರಿಹರಿಸಲಾಗಿದೆ: SVGAndImageFormField ಜೊತೆಗೆ ಜಾಂಗೊ ಇಮೇಜ್ ಕ್ಷೇತ್ರದಲ್ಲಿ 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>

ಇದು ಕಸ್ಟಮ್ ಇಮೇಜ್‌ಫೀಲ್ಡ್ ಆಗಿದ್ದು ಅದು ಇಮೇಜ್ ಅಥವಾ SVG ಅನ್ನು ಅಪ್‌ಲೋಡ್ ಮಾಡಲು ನಿಮಗೆ ಅನುಮತಿಸುತ್ತದೆ.
to_python ವಿಧಾನವು ಫಾರ್ಮ್‌ನಿಂದ ಡೇಟಾವನ್ನು ತೆಗೆದುಕೊಳ್ಳುತ್ತದೆ ಮತ್ತು ಅದನ್ನು ಫೈಲ್‌ಆಬ್ಜೆಕ್ಟ್ ಆಗಿ ಪರಿವರ್ತಿಸುತ್ತದೆ.
ಡೇಟಾವು ಈಗಾಗಲೇ ಫೈಲ್ ಆಬ್ಜೆಕ್ಟ್ ಆಗಿದ್ದರೆ, ಅದು ಅದನ್ನು ಹಿಂದಿರುಗಿಸುತ್ತದೆ.
ಡೇಟಾವು ಓದುವ ಗುಣಲಕ್ಷಣವನ್ನು ಹೊಂದಿದ್ದರೆ (ಅಂದರೆ ಅದು ಫೈಲ್ ತರಹದ ವಸ್ತುವಾಗಿದ್ದರೆ), ಅದು ಅದನ್ನು ಹಿಂತಿರುಗಿಸುತ್ತದೆ.
ಡೇಟಾ ಖಾಲಿಯಾಗಿದ್ದರೆ ("), ಅದು ಹಿಂತಿರುಗಿಸುತ್ತದೆ " (ಇದಕ್ಕೆ ಕಾರಣ ಖಾಲಿ ಫೈಲ್ ಅಪ್‌ಲೋಡ್ ಕ್ಷೇತ್ರಗಳು "ಇತರ ಕ್ಷೇತ್ರಗಳಂತೆ " ಬದಲಿಗೆ ಯಾವುದನ್ನೂ ಹಿಂತಿರುಗಿಸುವುದಿಲ್ಲ).
ಡೇಟಾ ಖಾಲಿಯಾಗಿಲ್ಲದಿದ್ದರೆ, ಅದನ್ನು ಫೈಲ್ ಆಗಿ ತೆರೆಯಲು ಪ್ರಯತ್ನಿಸುತ್ತದೆ ('rb' ಮೋಡ್ ಬಳಸಿ). ಇದು ಯಶಸ್ವಿಯಾದರೆ, ಅದು ಫೈಲ್ ವಸ್ತುವನ್ನು ಹಿಂತಿರುಗಿಸುತ್ತದೆ. ಇಲ್ಲದಿದ್ದರೆ, ಅದು ಕೇವಲ ಡೇಟಾವನ್ನು ಹಿಂದಿರುಗಿಸುತ್ತದೆ (ಇದು ನಂತರ ಈ ಡೇಟಾವನ್ನು ನಿರೀಕ್ಷಿಸುವ ಫಾರ್ಮ್‌ಗಳು ಮುರಿಯುವುದಿಲ್ಲ).

ಜಾಂಗೊ ನಿರ್ವಾಹಕರ ಮೂಲಕ ಇಮೇಜ್‌ಫೀಲ್ಡ್‌ಗೆ SVG ಫೈಲ್‌ಗಳನ್ನು ಅಪ್‌ಲೋಡ್ ಮಾಡಲು ಅನುಮತಿಸಿ

ಜಾಂಗೊದಲ್ಲಿನ ಇಮೇಜ್‌ಫೀಲ್ಡ್‌ಗೆ SVG ಫೈಲ್‌ಗಳನ್ನು ಅಪ್‌ಲೋಡ್ ಮಾಡಲು ನೀವು ಅನುಮತಿಸಲು ಬಯಸಿದರೆ, ನಿಮ್ಮ settings.py ಫೈಲ್‌ಗೆ ಈ ಕೆಳಗಿನ ಸಾಲನ್ನು ಸೇರಿಸುವ ಮೂಲಕ ನೀವು ಹಾಗೆ ಮಾಡಬಹುದು:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

ಜಾಂಗೊ ಇಮೇಜ್ ಫೀಲ್ಡ್‌ನಲ್ಲಿ Svgs ಅನ್ನು ಹೇಗೆ ಸಂಗ್ರಹಿಸುವುದು

ಜಾಂಗೊದಲ್ಲಿ svg ಗಳನ್ನು ಸಂಗ್ರಹಿಸಲು ಕೆಲವು ವಿಭಿನ್ನ ಮಾರ್ಗಗಳಿವೆ. ಇಮೇಜ್‌ಫೀಲ್ಡ್ ವರ್ಗವನ್ನು ಬಳಸುವುದು ಸರಳವಾದ ಮಾರ್ಗವಾಗಿದೆ. ಕ್ಷೇತ್ರಕ್ಕೆ ಮೌಲ್ಯವಾಗಿ URL ಅಥವಾ ಫೈಲ್‌ಪಾತ್ ಅನ್ನು ನಿರ್ದಿಷ್ಟಪಡಿಸಲು ಈ ವರ್ಗವು ನಿಮಗೆ ಅನುಮತಿಸುತ್ತದೆ.

ಜಾಂಗೊದಲ್ಲಿ svg ಗಳನ್ನು ಸಂಗ್ರಹಿಸುವ ಇನ್ನೊಂದು ವಿಧಾನವೆಂದರೆ ಇಮೇಜ್‌ಗ್ಯಾಲರಿ ವರ್ಗವನ್ನು ಬಳಸುವುದು. ಕ್ಷೇತ್ರಕ್ಕೆ ಮೌಲ್ಯವಾಗಿ URL ಅಥವಾ ಫೈಲ್‌ಪಾತ್ ಅನ್ನು ನಿರ್ದಿಷ್ಟಪಡಿಸಲು ಈ ವರ್ಗವು ನಿಮಗೆ ಅನುಮತಿಸುತ್ತದೆ ಮತ್ತು ಅದು ಆ ಮಾರ್ಗವನ್ನು ಆಧರಿಸಿ ಚಿತ್ರಗಳ ಗ್ಯಾಲರಿಯನ್ನು ಸ್ವಯಂಚಾಲಿತವಾಗಿ ರಚಿಸುತ್ತದೆ.

ಸಂಬಂಧಿತ ಪೋಸ್ಟ್ಗಳು:

ಒಂದು ಕಮೆಂಟನ್ನು ಬಿಡಿ