def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
profile = Profile.objects.get(user=request.user)
profile.education = form.cleaned_data['education']
profile.profession = form.cleaned_data['profession']
profile.employment = form.cleaned_data['employment']
avatar = request.FILES.get('avatar')
if avatar is not None:
fs = FileSystemStorage()
source_file = fs.save('cache/avatar.jpg', avatar)
img = Image.open('media/' + source_file)
width, height = img.size
if width >= height:
upper_x = int((width / 2) - (height / 2))
upper_y = 0
lower_x = int((width / 2) + (height / 2))
lower_y = height
else:
upper_x = 0
upper_y = int((height / 2) - (width / 2))
lower_x = width
lower_y = int((height / 2) + (width / 2))
box = (upper_x, upper_y, lower_x, lower_y)
img = img.crop(box)
img.save('media/'+source_file)
profile.avatar = fs.save('avatar/avatar.jpg', open('media/'+source_file, 'rb'))
os.remove('media/'+source_file)
profile.save()
return redirect("account:profile", username=request.user.username)
else:
return render(request, "account/edit.html", {"form": form})
评论列表
文章目录