Hans de Jong
Hans de Jong

Reputation: 2078

django upload image gives InMemoryUploadedFile error on validating

I am trying to make an upload image form. but it crashes on a succesfull clean in ym form (get 'InMemoryUploadedFile' object has no attribute 'get')

My form clean:

from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

def clean(self):
    content = self.cleaned_data['avatar']
    content_type = content.content_type.split('/')[0]
    if content_type in settings.CONTENT_TYPES:
        if content._size > settings.MAX_UPLOAD_SIZE:
            raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
    else:
        raise forms.ValidationError(_('File type is not supported'))
    return content

my setting attributes:

MAX_UPLOAD_SIZE = 1310720
CONTENT_TYPES = ['image']

The errors work fine. but when no ValidationError is found i get that InMemory error. What do i do wrong?

Upvotes: 0

Views: 771

Answers (1)

okm
okm

Reputation: 23871

You've returned self.cleaned_data['avatar'] from def clean() which should be def clean_avatar(). clean() should return the whole cleaned_data anyhow.

Upvotes: 1

Related Questions