前端上传图片base64,后台保存图片
# 前端上传的base64,存放在django的媒体文件库中 if avatar_url: b64_data = avatar_url.split(';base64,')[1] data = base64.b64decode(b64_data) image_url = os.path.join(MEDIA_ROOT_OLD, 'common/head_img/%s.jpg' % int(time.time())) with open(image_url, 'wb') as f: f.write(data) # 截取media路径,存放在字段中 image_url = image_url.split("project_name")[1].replace('\\', '/')[1:] # user对象 user.avatar_url = image_url