1
file_name = os.path.basename(image_url)
downloaded = urllib2.urlopen(image_url).read()
image_file = File(downloaded, name=file_name)
image_file.size = len(downloaded)
model = BlogPost()
model.image.save(file_name, image_file)
model.save()
型号:
类博文(models.Model): 图像= models.ImageField(upload_to =”博客图像”, help_text = '特征图像', 空白=真, 空=真)
我得到这个:
AttributeError: 'str' object has no attribute 'read'
如果我删除行image_file.size:
AttributeError: 'str' object has no attribute 'name'
downloaded
填充,因此它已成功下载的文件。
我在做什么错?