views.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:quora-django 作者: hndrxr 项目源码 文件源码
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})
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号