@login_required
@require_http_methods(['GET', 'POST'])
def edit_avatar(request):
 try:
  user_profile = Profile.objects.get(user=request.user)
 except Profile.DoesNotExist:
  user_profile = Profile.objects.create(user=request.user)
 if (request.method == 'POST'):
  old_avatar_path = None
  if (user_profile.avatar and os.path.isfile(user_profile.avatar.path)):
   old_avatar_path = user_profile.avatar.path
  form = AvatarForm(request.POST, request.FILES, instance=user_profile)
  if form.is_valid():
   if old_avatar_path:
    os.unlink(old_avatar_path)
   user_profile = form.save()
   content = _create_image_thumbnail(user_profile.avatar.path, settings.AVATAR_SIZE, pad=True)
   name = (user_profile.avatar.name + '.png')
   user_profile.avatar.delete()
   user_profile.avatar.save(name, content, save=True)
   return HttpResponseRedirect(reverse('users.edit_my_profile'))
 else:
  form = AvatarForm(instance=user_profile)
 return render(request, 'users/edit_avatar.html', {'form': form, 'profile': user_profile})

@ login_required @ require_http_methods ( [ 'GET' , 'POST' ] ) def edit_avatar ( request ) : try : user_profile = Profile . objects . get ( user = request . user ) except Profile . DoesNotExist : user_profile = Profile . objects . create ( user = request . user ) if ( request . method == 'POST' ) : old_avatar_path = None if ( user_profile . avatar and os . path . isfile ( user_profile . avatar . path ) ) : old_avatar_path = user_profile . avatar . path form = AvatarForm ( request . POST , request . FILES , instance = user_profile ) if form . is_valid ( ) : if old_avatar_path : os . unlink ( old_avatar_path ) user_profile = form . save ( ) content = _create_image_thumbnail ( user_profile . avatar . path , settings . AVATAR_SIZE , pad = True ) name = ( user_profile . avatar . name + '.png' ) user_profile . avatar . delete ( ) user_profile . avatar . save ( name , content , save = True ) return HttpResponseRedirect ( reverse ( 'users.edit_my_profile' ) ) else : form = AvatarForm ( instance = user_profile ) return render ( request , 'users/edit_avatar.html' , { 'form' : form , 'profile' : user_profile } )
edit group avatar .