Read and Write Files in Django and Python

File 和 ImageFile 接受 Python 的 file 或 StringIO 物件 而 ContentFile 接受 string ref: https://docs.djangoproject.com/en/dev/ref/files/file/#the-file-object Django Form image_file = request.FILES['file'] # 方法一 profile.mugshot.save(image_file.name, image_file) # 方法二 profile.mugshot = image_file profile.save() open('/path/to/file.png') from django.core.files import File with open('/home/vinta/image.png', 'rb') as f: profile.mugshot = File(f) profile.save() Django ContentFile import os import uuid from django.core.files.base import ContentFile… Read More