这是我尝试过的:
r = requests.get(img_url)
temp = NamedTemporaryFile(delete=True)
temp.write(r.content)
temp.flush()
image = Image()
image.image.save('testimagefilename', File(temp), save=True)
image.save()
在
image.image.save()
行上,错误跟踪为:Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 90, in save
name = self.field.generate_filename(self.instance, name)
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 332, in generate_filename
return os.path.join(self.get_directory_name(), self.get_filename(filename))
File "/usr/local/lib64/python3.4/site-packages/django/db/models/fields/files.py", line 322, in get_filename
return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
TypeError: get_valid_name() missing 1 required positional argument: 'name'
这是因为Django需要本地操作系统路径吗?我正在使用S3BotoStorage,ImageField正在使用与项目其余部分不同的存储桶:
image = models.ImageField(storage=S3CustomStorage)
class S3CustomStorage(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket'] = getattr(settings, 'AWS_BUCKET_CUSTOM')
super(S3CustomStorage, self).__init__(*args, **kwargs)
最佳答案
也许看到Trying to understand Django source code and cause of missing argument TypeError并更改
image = models.ImageField(storage=S3CustomStorage)
至
image = models.ImageField(storage=S3CustomStorage())
我有相同的TypeError ;-),但在我的情况下,它可以与其他()一起使用
关于Django image.save()TypeError : get_valid_name() missing positional argument: 'name' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34098434/