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

  • SEARCH
  • Home
  • 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 5941353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:05:52+00:00 2026-05-22T16:05:52+00:00

I tried hard, but I couldn’t make it. I am trying to find a

  • 0

I tried hard, but I couldn’t make it. I am trying to find a way on how a user can upload its avatar image, using django and this is my code:

settings.py

USER_IMAGE_PATH = '/site_media/user_data/images/'

models.py

fs = FileSystemStorage(location=settings.USER_IMAGE_PATH)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    avatar = models.ImageField(storage=fs, upload_to=settings.USER_IMAGE_PATH)

forms.py

class ProfileForm(forms.Form):
    avatar = forms.ImageField(label='avatar', required=False)

urls.py

(r'^profile/(\w+)/$', profile_form),

views.py

@login_required
def profile_form(request, username):
    form = ProfileForm(request.POST, request.FILES)
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        raise Http404(u'User not Found')
    if form.is_valid():
        avatar = form.cleaned_data['avatar']
        profile = user.get_profile()
        profile.avatar = avatar
        if not avatar:
            pass
        else:
            image_file = request.FILES['avatar']
            im = imageilb()
            filename, content = im.handle_uploaded_image(image_file)
            profile.avatar.save(filename, content)

imagelib.handle_uploaded_image()

import hashlib
from PIL import Image
import os.path
import StringIO
import django

def handle_uploaded_image(self,i):
    # resize image
    imagefile  = StringIO.StringIO(i.read())
    imageImage = Image.open(imagefile)

    (width, height) = imageImage.size
    (width, height) = self.scale_dimensions(width, height, longest_side=240)

    resizedImage = imageImage.resize((width, height))

    imagefile = StringIO.StringIO()
    resizedImage.save(imagefile,'JPEG')
    filename = hashlib.md5(imagefile.getvalue()).hexdigest()+'.jpg'

    # #save to disk - hardcoding of paths will be removed as soon as this thing works
    imagefile = open(os.path.join('/site_media/user_data/images',filename), 'w')
    resizedImage.save(imagefile,'JPEG')
    imagefile = open(os.path.join('/site_media/user_data/images',filename), 'r')
    return filename, django.core.files.File(imagefile)

With this code, the field in the database is inserted as the path of the saved image, correctly (although just the name of the image-file would be enough) but the image is not saved where it is supposed to be saved or not saved at all (I can’t be sure if the file is stored somewhere else that is why I am saying this — though I checked)
any more code will be added if requested.

What am I doing wrong?

  • 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-22T16:05:53+00:00Added an answer on May 22, 2026 at 4:05 pm

    Hope my suggestion be helpful for you

    settings.py

    USER_IMAGE_PATH = '/site_media/user_data/images/'
    

    models.py

    fs = FileSystemStorage(location=settings.USER_IMAGE_PATH)
    class UserProfile(models.Model):
        user = models.ForeignKey(User, unique=True)
        avatar = models.ImageField(storage=fs, upload_to='avatar')
    
        def save(self, *args, **kwargs):
           super(UserProfile, self).save(*args, **kwargs)
           if self.avatar:
               imglib.resize_image(avatar)
    

    imglib.py

    from PIL import Image
    
    def resize_image(avatar, size=(200,200)):
        image = Image.open(avatar.path)
        image.resize(size, Image.ANTIALIAS).save(avatar.path, 'JPEG', quality=75)
    

    views.py

    @login_required
    def profile_form(request, username):
        form = ProfileForm(request.POST, request.FILES)
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            raise Http404(u'User not Found')
        if form.is_valid():
            avatar = form.cleaned_data['avatar']
            profile = user.get_profile()
            profile.avatar = avatar
            profile.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Please close this thread if duplicate, I tried hard, but couldn't find any matching
Pardon my title guys. I tried thinking hard but couldn't think of better title.
I tried to look answers but I couldn't find any. So I want to
Modifying CKEditor 3.6.2 is not easy, but I tried hard . One problem that
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried a bunch of things but I can't get it to work consistently amid
I tried hard but still could not get , for what purpsoe , i
I'm trying to learn Python through Learn Python the Hard Way. I'm now on
I tried searching Google and this site for this, but it was hard to
I understand this question couldn't be more beginner, but I am having a hard

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.