def goodsinsert(request): try: myfile = request.FILES.get("picname", None) if not myfile: return HttpResponse("没有上传文件信息!") filename = str(time.time()) + "." + myfile.name.split('.').pop() destination = open(os.path.join("./static/goods/", filename), 'wb+') for chunk in myfile.chunks(): destination.write(chunk) destination.close() im = Image.open("./static/goods/" + filename) im.thumbnail((206, 206)) im.save("./static/goods/" + filename, 'jpeg') im.thumbnail((150, 150)) im.save("./static/goods/m_" + filename, 'jpeg') im.thumbnail((60, 60)) im.save("./static/goods/s_" + filename, 'jpeg') ob = Goods() ob.typeid = request.POST['typeid'] ob.goods = request.POST['goods'] ob.company = request.POST['company'] ob.picname = filename ob.descr = request.POST['descr'] ob.price = request.POST['price'] ob.state = request.POST['state'] ob.store = request.POST['store'] ob.num = request.POST['num'] ob.clicknum = request.POST['clicknum'] ob.addtime = time.time() ob.save() context = {'info': '商品添加成功'} except: context = {'info': '商品添加失败'} return render(request, "myadmin/goods/info.html", context)
def goodsinsert(request): try: # 判断并执行图片上传,缩放等处理 myfile = request.FILES.get("pic", None) if not myfile: return HttpResponse("没有上传文件信息!") # 以时间戳命名一个新图片名称 filename = str(time.time()) + "." + myfile.name.split('.').pop() destination = open(os.path.join("./static/myadmin/goods", filename), 'wb+') for chunk in myfile.chunks(): # 分块写入文件 destination.write(chunk) destination.close() # 执行图片缩放 im = Image.open("./static/myadmin/goods/" + filename) # 缩放到375*375: im.thumbnail((375, 375)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/goods/" + filename, None) # 缩放到220*220: im.thumbnail((220, 220)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/goods/m_" + filename, None) # 缩放到220*220: im.thumbnail((100, 100)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/goods/s_" + filename, None) ob = Goods() ob.typeid = request.POST['typeid'] ob.goods = request.POST['goods'] ob.company = request.POST['company'] ob.descr = request.POST['descr'] ob.price = request.POST['price'] ob.picname = filename ob.state = request.POST['state'] ob.store = request.POST['store'] ob.num = request.POST['num'] ob.clicknum = request.POST['clicknum'] ob.addtime = time.time() ob.save() context = {'info': '添加成功'} except: context = {'info': '添加失败'} return render(request, 'myadmin/info.html', context)
def insertgoods(request): try: #判断执行图片上传 myfile = request.FILES.get('picname', None) if not myfile: return HttpResponse('没有上传图片信息') #时间戳命名图片 filename = str(time.time()) + '.' + myfile.name.split('.').pop() destination = open(os.path.join('./static/goods/', filename), 'wb+') for chunk in myfile.chunks(): destination.write(chunk) destination.close() #图片缩放 im = Image.open('./static/goods/' + filename) im.thumbnail((375, 375)) im.save('./static/goods/' + filename, None) im.thumbnail((220, 220)) im.save('./static/goods/m_' + filename, None) im.thumbnail((100, 100)) im.save('./static/goods/s_' + filename, None) ob = Goods() ob.typeid = request.POST['typeid'] ob.goods = request.POST['goods'] ob.company = request.POST['company'] ob.price = request.POST['price'] ob.picname = filename ob.num = request.POST['num'] ob.clicknum = request.POST['clicknum'] ob.descr = request.POST['descr'] ob.store = request.POST['store'] ob.status = request.POST['status'] ob.addtime = time.time() ob.save() context = {'info': '添加成功!'} except: context = {'info': '添加失败!'} return render(request, "myadmin/info.html", context)
def goodsinsert(request): try: myfile = request.FILES.get('pic',None) if not myfile: return HttpResponse('没有上传图片') filename = str(time.time())+'.'+myfile.name.split('.').pop() destination = open(os.path.join('./static/goods',filename),'wb+') for chunk in myfile.chunks(): destination.write(chunk) destination.close() im = Image.open('./static/goods/'+filename) im.thumbnail((800,800)) im.save('./static/goods/x_'+filename,'jpeg') im.thumbnail((412,412)) im.save('./static/goods/'+filename,'jpeg') im.thumbnail((200,200)) im.save('./static/goods/m_'+filename,'jpeg') im.thumbnail((100,100)) im.save('./static/goods/s_'+filename,'jpeg') goods = Goods() goods.typeid =request.POST['typeid'] goods.goods =request.POST['goods'] goods.company =request.POST['company'] goods.price =request.POST['price'] goods.picname =filename goods.state =request.POST['state'] goods.store =request.POST['store'] goods.descr =request.POST['descr'] goods.num =0 goods.clicknum =0 goods.addtime =time.time() goods.save() context = {'info':'添加成功'} except: context = {'info':'添加失败'} return render(request,'myadmin/info.html',context)