- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 PIL 压缩上传的图像 (FileField
)。但是我收到一个错误,我认为这是双重保存的问题? (保存我的图像,然后保存包含图像的整个表格)。我想在保存图像时执行 commit=False
但它似乎不可行。这是我的代码:
...
if form_post.is_valid():
instance = form_post.save(commit=False)
instance.user = request.user
if instance.image:
filename = instance.image
instance.image = Image.open(instance.image)
instance.image.thumbnail((220, 130), Image.ANTIALIAS)
instance.image.save(filename, quality=60)
instance.save()
返回 'JpegImageFile' object has no attribute '_committed'
最后一行错误 (instance.save()
)
有人能找出问题所在吗? - 知道我该如何解决吗?
完整回溯:
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/zorgan/Desktop/project/site/post/views.py" in post
68. if uploaded_file_type(instance) is True:
File "/Users/zorgan/Desktop/project/site/functions/helper_functions.py" in uploaded_file_type
12. f = file.image.read(1024)
Exception Type: AttributeError at /post/
Exception Value: 'JpegImageFile' object has no attribute 'read'
完整模型:
class Post(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
title = models.TextField(max_length=95)
image = models.FileField(null=True, blank=True)
和随附的 PostForm
:
class PostForm(forms.ModelForm):
title = forms.TextInput(attrs={'placeholder': 'title'})
class Meta:
model = Post
fields = [
'user',
'title',
'image',
]
views.py
def post(request):
if request.user.is_authenticated():
form_post = PostForm(request.POST or None, request.FILES or None)
if form_post.is_valid():
instance = form_post.save(commit=False)
if instance.image:
filename = instance.image
instance.image = Image.open(instance.image)
instance.image.thumbnail((220, 130), Image.ANTIALIAS)
instance.image.save(filename, quality=60)
instance.save()
return HttpResponseRedirect('/home/')
else:
form_post = PostForm()
context = {
'form_post': form_post,
}
return render(request, 'post/post.html', context)
else:
return HttpResponseRedirect("/accounts/signup/")
以下代码:
if instance.image:
im = Image.open(instance.image)
print("Filename:", im.filename) #doesn't print anything
thumb = im.thumbnail((220, 130), Image.ANTIALIAS)
thumb.save(im.filename, quality=60)
返回一个 AttributeError
:'NoneType' 对象没有属性 'save'
。我相信这是因为 im.filename
没有打印任何东西。知道为什么吗?
另一种方法:
if instance.image:
im = Image.open(instance.image)
thumb = im.thumbnail((220, 130), Image.ANTIALIAS)
thumb_io = BytesIO()
thumb.save(thumb_io, im.format, quality=60)
instance.image.save(im.filename, ContentFile(thumb_io.get_value()), save=False)
还返回 AttributeError
:'NoneType' object has no attribute 'save'
,在这一行:thumb.save(thumb_io, im.format, quality =60)
。不确定为什么?
最佳答案
您必须将 django 的 File
对象实例传递给 FileField.save()
更改文件字段的内容。它的工作方式与其他类型的模型字段略有不同。
FieldFile.save(name, content, save=True)
This method takes a filename and file contents and passes them to thestorage class for the field, then associates the stored file with themodel field. If you want to manually associate file data withFileField instances on your model, the save() method is used topersist that file data.
from PIL import Image
from django.core.files.base import ContentFile
if instance.image:
im = Image.open(instance.image)
im.thumbnail((220, 130), Image.ANTIALIAS)
thumb_io = BytesIO()
im.save(thumb_io, im.format, quality=60)
instance.image.save(im.filename, ContentFile(thumb_io.getvalue()), save=False)
instance.save()
但是如果您没有使用远程文件存储后端,您可以只覆盖文件本身。该文件是在您调用 form.save()
时创建的。由于您使用的是相同的文件名和位置,因此您实际上不必触摸模型或告诉 django 您正在弄乱文件本身。
if instance.image:
im = Image.open(instance.image)
im.thumbnail((220, 130), Image.ANTIALIAS)
im.save(im.filename, quality=60)
关于python - 使用 PIL 时 'JpegImageFile' 对象没有属性 '_committed' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49227183/
我正在尝试将我的图片调整为 124*124 大小。我搜索了很多并找到了一些相关问题的解决方案。但即使我的问题没有解决。 我的模型 class MyUsers(models.Model): us
我有一个保存图像的表单,一切正常,但我希望能够裁剪图像。但是,当我使用 Pillow 执行此操作时,出现了一个奇怪的错误,该错误并没有真正让我继续下去。 Attribute error at /use
在研究尽可能快地写入强制文件时,我在几个地方看到了以下代码块: _commit(outputFile.rdbuf()->fd()); 其中 outputFile 是一个 std::ofstream 对
在这里,我试图更新特定产品的每个产品图像。但它不能正常工作。这里只有第一个对象的图像正在更新。 有一个模板,我们可以在其中一次更新产品和产品图片。 ProductImage有一个 ManyToOne与
我重写了保存方法,如下所示,并使用 get_thumbnails 和 sorl 保存.. 但是收到错误'ImageFile'对象没有属性'_commissed' class HotelPhotos(m
好吧,我有一个字段类型 FileField,当我尝试删除它的内容时,会返回此错误消息:“bool”对象没有属性“_committed”`上传工作正常,问题仅出现在我尝试删除已发送的文件时。 我的看法:
我的模型中有一个 DjangoFileField。我正在尝试将音频类型从该 FielField 转换为 mp3,然后再次尝试保存它。但是在转换类型并使用 pydub 导出它之后,它返回以下错误 Att
我有一个 Django 项目,其中多个进程正在访问后端 mysql 数据库。一个进程正在创建记录,而第二个进程正在尝试读取这些记录。我遇到一个问题,在我手动调用 connection._commit(
我正在使用 PIL 压缩上传的图像 (FileField)。但是我收到一个错误,我认为这是双重保存的问题? (保存我的图像,然后保存包含图像的整个表格)。我想在保存图像时执行 commit=False
我正在使用 requests 通过链接下载图像并将它们保存到模型中 f = open('00000001.jpg','wb') f.write(requests.get('http://www.gun
我正在尝试为需要 django.db.models.ImageField 的测试用例设置数据。我正在尝试使用来自 factory-boy 的 factory.django.ImageField,但出现
我是一名优秀的程序员,十分优秀!