在使用django时不知道怎么保存图片,又不想用它的form ,在网上找了许久,终于找到个解决方案,利用PIL.image 将POST上来的图片保存到media目录下,然后再修改models
from PIL import Image screen_name = request.POST.get('screen_name') mail =request.POST.get('mail') gender = request.POST.get('gender') img = request.FILES.get('image') img_file = Image.open(img) #保存图片 path = MEDIA_ROOT + '/profile_img/' file = request.user.username + '_profile.jpg' img_file.thumbnail((200,200),Image.ANTIALIAS) img_file.save(path + file) #更改用户信息 user_info = UsersInfo.objects.get(username=request.user.username) user_info.profile_img = '/media/profile_img/'+ file user_info.save()