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("picname", None) if not myfile: return HttpResponse('没有图片上传信息') #为上传的图片取名 filename = str(time.time()) + "." + myfile.name.split('.').pop() #将图片保存到static中 destination = open("./static/myadmin/img/" + filename, "wb+") #写入图片 for chunk in myfile.chunks(): destination.write(chunk) destination.close() #执行图片的缩放 im = Image.open("./static/myadmin/img/" + filename) # 缩放到375*375: im.thumbnail((375, 375)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/img/" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((220, 220)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/img/m_" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((100, 100)) # 把缩放后的图像用jpeg格式保存: im.save("./static/myadmin/img/s_" + filename, 'jpeg') # #缩放到75*75 # im.thumbnail((75,75)) # #把所放后的图像用jpeg格式保存 # im.save("./static/myadmin/img/s_"+filename,'jpeg') #执行信息的添加 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 = 1 ob.store = request.POST['store'] 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) # 缩放到375*375: im.thumbnail((375, 375)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/XL_" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((220, 220)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/L_" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((40, 40)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/S_" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((75, 75)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/M_" + filename, 'jpeg') # 获取商品信息并执行添加 ob = Goods() ob.goods = request.POST['goods'] ob.typeid = request.POST['typeid'] ob.company = request.POST['company'] ob.price = request.POST['price'] ob.store = request.POST['store'] ob.descr = request.POST['descr'] ob.picname = filename ob.state = 1 ob.addtime = time.time() ob.save() context = {'info': '添加成功!'} except: context = {'info': '添加失败!'} return render(request, "myadmin/info.html", context)
def goods_insert(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+') 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, 'jpeg') #缩放220*220 im.thumbnail((220, 220)) #把缩放的图像用jpeg格式保存 im.save('./static/myadmin/goods/m_' + filename, 'jpeg') #缩放100*100 im.thumbnail((100, 100)) #把缩放的图像用jpeg格式保存 im.save('./static/myadmin/goods/s_' + filename, 'jpeg') #获取信息并执行添加 ob = Goods() ob.goods = request.POST['goods'] ob.typeid = request.POST['typeid'] ob.company = request.POST['company'] ob.price = request.POST['price'] ob.store = request.POST['store'] ob.descr = request.POST['descr'] ob.picname = filename ob.state = 1 ob.addtime = time.time() ob.save() context = {'info': '添加成功'} # except: # context = {'info':'添加失败'} return render(request, 'myadmin/info.html', context)
def goodsinsert(request): try: commodity = Goods() commodity.typeid = request.POST['typeid'] commodity.goods = request.POST['name'] commodity.company = request.POST['company'] commodity.descr = request.POST['descr'] commodity.price = request.POST['price'] imgfile = request.FILES.get("imgfile", None) #图片使用时间戳命名并存入到指定文件 imgname = str('{:.4f}'.format( time.time())) + '.' + imgfile.name.split('.').pop() destination = open(os.path.join("./static/imgs/goods/", imgname), 'wb+') for chunk in imgfile.chunks(): destination.write(chunk) destination.close() commodity.picname = imgname #生成不同的缩放图片 im = Image.open("./static/imgs/goods/" + imgname) #2601*261 im.thumbnail((261, 261)) im.save("./static/imgs/goods/" + 'm' + imgname) #100*100 im.thumbnail((100, 100)) im.save("./static/imgs/goods/" + 'l' + imgname) commodity.state = request.POST['state'] commodity.store = request.POST['number'] commodity.addtime = time.strftime('%Y%m%d%H%M', time.localtime()) commodity.save() info = "添加成功" except: info = "添加失败" if imgfile: os.remove(os.path.join("./static/imgs/goods/", imgname)) os.remove(os.path.join("./static/imgs/goods/", 'm' + imgname)) os.remove(os.path.join("./static/imgs/goods/", 'l' + imgname)) content = {"info": info} return render(request, 'myadmin/info.html', content)
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)