Exemplo n.º 1
0
def InsertUser(request):
    try:
        user=User()
        user.title=request.POST['title']
        photo=request.FILES.get('mypic',None)

        user.photo=photo.name
        if not photo:
            return HttpResponse("没有上传图片")
        filename = str(photo.name)
        destination = open("./static/myapp/pics/" + filename, "wb+")
        for chunk in photo.chunks():
            destination.write(chunk)
        destination.close()

        # 执行图片缩放
        im = Image.open("./static/myapp/pics/" + filename)
        # 缩放到75*75(缩放后的宽高比例不变)
        im.thumbnail((192, 120))
        # 把缩放后的图片用jpeg格式保存
        im.save("./static/myapp/pics/s_" + filename, None)

        user.createtime=datetime.now()
        user.save()
        context = {"info": "添加成功"}

    except:
        context = {"info": "添加失败"}
    return render(request,'myapp/info.html',context)