Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » newforms image upload and validation [RSS Feed]

#1 March 26, 2008 22:15:34

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

newforms image upload and validation


I'm using an avatar imagefiled in the model and have defined an
imagefield in the registration form I'm using for the user to sign up.
avatar is not a required field (both in the form as well as in the
model)


I've got validation included in the Registration Form for the uploaded
image size:

def clean_avatar(self):

size = len(self.cleaned_data.content)
if size > 500 * 1024:
raise forms.ValidationError('Size exceeded allowable
limit')

return self.cleaned_data


All works well if the user does upload an image. If no image is
uploaded, I get a
"NoneType' object has no attribute 'content' " exception.

Which I understand why -- because no file has been uploaded and so
there is no content attribute present.

however, I'm not sure how I can first check if a file has been
uploaded by user or not..and then have the above clean method be
invoked only when a file has been uploaded...if not, skip validating
the input.

any ideas ?
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 27, 2008 03:08:03

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

newforms image upload and validation


On Wed, Mar 26, 2008 at 5:15 PM, <> wrote:

>
> I'm using an avatar imagefiled in the model and have defined an
> imagefield in the registration form I'm using for the user to sign up.
> avatar is not a required field (both in the form as well as in the
> model)
>
>
> I've got validation included in the Registration Form for the uploaded
> image size:
>
> def clean_avatar(self):
>
> size = len(self.cleaned_data.content)
> if size > 500 * 1024:
> raise forms.ValidationError('Size exceeded allowable
> limit')
>
> return self.cleaned_data
>
>
> All works well if the user does upload an image. If no image is
> uploaded, I get a
> "NoneType' object has no attribute 'content' " exception.
>
> Which I understand why -- because no file has been uploaded and so
> there is no content attribute present.
>
> however, I'm not sure how I can first check if a file has been
> uploaded by user or not..and then have the above clean method be
> invoked only when a file has been uploaded...if not, skip validating
> the input.
>
> any ideas ?
>

I don't believe you can bypass your clean method in the case where no file
was uploaded. Rather you should change your clean_avatar function to handle
the case of no file being uploaded. That is, only do the size check if
self.cleaned_data is not None.

Karen

--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 27, 2008 04:31:53

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

newforms image upload and validation


thanks karen..guess I'm overworked -- I was trying out something like
"if self.cleaned_data == ' ' " ....failed to notice that the
ImageField is 'None' if empty...

thanks a bunch..you made my day :-)

-pd.

On Mar 26, 10:07 pm, "Karen Tracey" <> wrote:
> On Wed, Mar 26, 2008 at 5:15 PM, <> wrote:
>
> > I'm using an avatar imagefiled in the model and have defined an
> > imagefield in the registration form I'm using for the user to sign up.
> > avatar is not a required field (both in the form as well as in the
> > model)
>
> > I've got validation included in the Registration Form for the uploaded
> > image size:
>
> > def clean_avatar(self):
>
> > size = len(self.cleaned_data.content)
> > if size > 500 * 1024:
> > raise forms.ValidationError('Size exceeded allowable
> > limit')
>
> > return self.cleaned_data
>
> > All works well if the user does upload an image. If no image is
> > uploaded, I get a
> > "NoneType' object has no attribute 'content' " exception.
>
> > Which I understand why -- because no file has been uploaded and so
> > there is no content attribute present.
>
> > however, I'm not sure how I can first check if a file has been
> > uploaded by user or not..and then have the above clean method be
> > invoked only when a file has been uploaded...if not, skip validating
> > the input.
>
> > any ideas ?
>
> I don't believe you can bypass your clean method in the case where no file
> was uploaded. Rather you should change your clean_avatar function to handle
> the case of no file being uploaded. That is, only do the size check if
> self.cleaned_data is not None.
>
> Karen
--~--~---------~--~----~------------~-------~--~----~
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
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » newforms image upload and validation [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support