Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:46:16+00:00 2026-05-22T19:46:16+00:00

I have a form where a user uploads an avatar, and it resizes the

  • 0

I have a form where a user uploads an avatar, and it resizes the photo and reloads the page with the new avatar. The form is working perfectly without any validation.

When I add a validation to raise an error if the image is below a certain size, the forms.ValidationError works fine. However, when the data does pass validation, it causes the form to error.

Here is what I currently have —

def handle_uploaded_image(i):
   ### enter size of thumbnail, returns (filename, content)        

def getting_started_pic(request):
    form = ProfilePictureForm()
    username = request.session.get('username')
    profile = UserProfile.objects.get(user=username) 
    if request.method == 'POST':
        form = ProfilePictureForm(request.POST, request.FILES)
        if form.is_valid():
            form = ProfilePictureForm(request.POST, request.FILES, instance = profile)
            ob = form.save(commit=False)
            try:
                t = handle_uploaded_image(request.FILES['avatar'])
                ob.avatar.save(t[0],t[1])
            except KeyError:
                ob.save()
            return render_to_response (...)
    return render_to_response (...)

And in models.py —

class ProfilePictureForm(ModelForm):
    avatar = forms.ImageField()
    class Meta:
        model = UserProfile
        fields = ('avatar')

    def clean_avatar(self):
        import StringIO
        from PIL import Image, ImageOps        

        str=""
        for c in self.cleaned_data['avatar'].chunks():
            str += c

        imagefile = StringIO.StringIO(str)
        image = Image.open(imagefile)

        width, height = image.size[0], image.size[1]
        if width < 200 or height < 200:
            raise forms.ValidationError("Please upload an image at least 200 pixels wide.")
        else:
            return self.cleaned_data['avatar']

So when I use this validation and it returns cleaned_data, it throws the following error:

The UserProfile could not be changed because the data didn't validate.

From the traceback, the line throwing the error is: ob = form.save(commit=False), so it seems like a model-level validation error. Could you please tell me why this error is occurring, and how I can fix it? Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:46:17+00:00Added an answer on May 22, 2026 at 7:46 pm

    Might it be this:

    form = ProfilePictureForm(request.POST, request.FILES)
    if form.is_valid():
        form = ProfilePictureForm(request.POST, request.FILES, instance = profile)
        ...
    

    Once your form validates, you overwrite the form with a new ModelForm that is created from the instance already in the database. This will get rid of any reference to what you have just uploaded, so it won’t validate?

    Try it with just:

    form = ProfilePictureForm(request.POST, request.FILES, instance = profile)
    if form.is_valid():
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form on a page where the user has inputs to edit
I have a form a user can enter their name, then it will add
On a website if I have a form where the user can input some
I have a form which takes both the user details and an image uploaded
We have a form that allows a user to filter a list with by
I have a form in which the user can choose a component type from
I have to display a column chart in a user form in VBA. Exporting
I have a form with a Clear button. When the user clicks Clear, I
I have a search form and result list. The form allows the user to
In my current application I have a form that requires the user to enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.