django: gave up on user uploading its avatar. Image is not stored. Need help!
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
im开发者_JAVA技巧port 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?
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()
You might want to doublecheck the opening slash in your paths (I assume you don't actually want to save images in the root of your file system.)
i.e.
'/site_media/...'
精彩评论