Hi, I haven't much forms with image/files, but I believe you should do
something like this:
photo = self.cleaned_data.get('photo')
This is for your second version.

When doing stuff like this I find it sometimes can be helpful to
insert an error like just typing raise. Django will in the debug
screen let you look at your variables. So you could see what you fx
are getting when you do your photo = ... and see why it raised that
error.

Hope it helps
Briel

On 14 Feb., 10:52, PeteDK <petermoel...@gmail.com> wrote:
> Hi :)
>
> I'm trying to validate an uploaded image's size and filetype.
> However i have problems even gettings to the validation as django
> keeps telling me that there's no object.
> When i don't use the validation part I can upload an image just fine.
> So the upload thing works.
>
> I just can't figure out how to pass on the file to the validation
> part.
> I have tried with several approaches I have found on the internet.
> Therefore i will list my view, model and the two form methods i have
> tried.
>
> first, my view:
>
> def upload_file(request):
>         myprofile = request.user.get_profile()
>         current = request.user
>         print "upload 1"
>         if request.method == 'POST':
>                 form = AvatarForm(request.POST, 
> request.FILES,instance=myprofile)
>                 print "upload 2"
>                 if form.is_valid():
> [...]
>
> model:
> class Avatar(models.Model):
>         user = models.ForeignKey(User,unique=True,editable=False)
>         photo = models.ImageField(upload_to=get_profile_path, blank=True,
> null=True)
>         thumbnail = models.ImageField(upload_to='profile_thumb', blank=True,
> null=True)
>
> form:
>
> class AvatarForm(ModelForm):
>
>         class Meta:
>                 model = Avatar
>
> #1
>
>         def clean(self,data,initial=None):
>                 content = super(ImageField, self).clean(self,data,initial)
>                 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
> #2
>         def clean(self):
>                 if 'photo' in self.cleaned_data:
>                         photo = self.cleaned_data['photo']
>                         print photo
>                         if photo.get('content-type') != 'image/jpeg':
>                                 msg = 'Only .ZIP archive files are allowed.'
>                                 raise forms.ValidationError(msg)
>                                 return photo
>
> The error i get is pretty much the same with both methods.  Nonetype
> has nu method/attribute: content_type/get... So i don't know if the
> rest of the validation code works since I can't pass my file through
> to the validation :-(
>
> Any help is appreciated :-)
>
> And a good weekend to all of you
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to