, Я исправил код в своем ответе сейчас.

ользую 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' object has no attribute '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), Не уверен, почему, хотя?

Ответы на вопрос(1)

Ваш ответ на вопрос