แก้ไขแล้ว: วิธีจัดเก็บ svgs ในฟิลด์รูปภาพ django ด้วย SVGAndImageFormField

ปัญหาหลักคือ 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>

นี่คือ ImageField แบบกำหนดเองที่ให้คุณอัปโหลดรูปภาพหรือ SVG
เมธอด to_python นำข้อมูลจากฟอร์มและแปลงเป็น FileObject
หากข้อมูลเป็น FileObject อยู่แล้ว ก็จะส่งกลับข้อมูลนั้น
หากข้อมูลมีแอตทริบิวต์ read (เช่น หากเป็นอ็อบเจกต์คล้ายไฟล์) ก็จะส่งกลับค่านั้น
หากข้อมูลว่างเปล่า (”) ข้อมูลจะส่งคืน ” (เนื่องจากฟิลด์อัปโหลดไฟล์ที่ว่างเปล่าจะส่งคืน None แทน ” เช่นเดียวกับฟิลด์อื่นๆ)
หากข้อมูลไม่ว่างเปล่า ข้อมูลจะพยายามเปิดเป็นไฟล์ (โดยใช้โหมด 'rb') หากสำเร็จก็จะส่งกลับวัตถุไฟล์ ถ้าไม่ ก็จะส่งคืนข้อมูลเอง (เพื่อให้แบบฟอร์มที่อาจคาดว่าจะใช้ข้อมูลนี้ในภายหลังไม่เสียหาย)

อนุญาตให้อัปโหลดไฟล์ SVG ไปยัง ImageField ผ่านผู้ดูแลระบบ Django

หากคุณต้องการอนุญาตให้อัปโหลดไฟล์ SVG ไปยัง ImageField ใน Django คุณสามารถทำได้โดยเพิ่มบรรทัดต่อไปนี้ในไฟล์ settings.py ของคุณ:

IMAGE_FIELD_MAX_FILE_SIZE = 1000000

วิธีจัดเก็บ Svgs ในฟิลด์รูปภาพ Django

มีหลายวิธีในการจัดเก็บ svgs ใน Django วิธีที่ง่ายที่สุดคือการใช้คลาส ImageField คลาสนี้อนุญาตให้คุณระบุ URL หรือพาธไฟล์เป็นค่าสำหรับฟิลด์

อีกวิธีในการจัดเก็บ svgs ใน Django คือการใช้คลาส ImageGallery คลาสนี้อนุญาตให้คุณระบุ URL หรือพาธของไฟล์เป็นค่าสำหรับฟิลด์ และคลาสนี้จะสร้างแกลเลอรีรูปภาพตามพาธนั้นโดยอัตโนมัติ

กระทู้ที่เกี่ยวข้อง:

แสดงความคิดเห็น