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 post(self, request): try: goods_post = request.POST gadd = Goods() gadd.title = goods_post.get('goods_name') # 商品标题 gadd.price = goods_post.get('goods_price') # 商品价格 gadd.storage = goods_post.get('goods_storage') # 商品库存 gadd.status = goods_post.get('goods_status') # 商品状态 gadd.info = goods_post.get('goods_info') # 商品详情 gadd.typeid = Types.objects.get( id=goods_post.get('goods_type_id')) # 分类级别 # 导入图片文件 from my_public_package.my_models import filesload gadd.pic = filesload(request, 'goods_images', './static/myadmin/goods_images/') # 商品图片 gadd.save() return HttpResponse( '<script>alert("添加成功");location.href="/myadmin/goods_index"</script>' ) except: return HttpResponse( '<script>alert("添加失败");location.href="/myadmin/goods_add"</script>' )
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_doadd(request): try: goods = Goods() select_1 = request.POST.get('select_1') select_2 = request.POST.get('select_2') select_3 = request.POST.get('select_3') if select_3: type_3 = int(select_3) t3 = get_type(type_3)[0] goods.type_id = t3 elif select_2 and select_3 == "": type_2 = int(select_2) t2 = get_type(type_2)[0] goods.type_id = t2 else: type_1 = int(select_1) t1 = get_type(type_1)[0] goods.type_id = t1 goods.name = request.POST.get('goods') goods.company = request.POST.get("company") goods.price = request.POST.get("price") goods.stock = request.POST.get("store") goods.describe = request.POST.get("descr") # 这里注意使用的是request.FILES pic = request.FILES.get("pic") if pic: suffix = pic.name.split('.')[-1:] for sf in suffix: if sf.lower() in ['jpg','png']: # 格式化文件 filename = str(time.strftime("%Y%m%d%H%M%S",time.localtime())) + "." + sf goods.picture = filename upload_path = settings.STATICFILES_DIRS[0] + os.path.sep + "goods" + os.path.sep + filename # 以二进制的方式把图片从内存写入到服务器 with open(upload_path, 'wb+') as f: # pic.chunks()循环读取图片内容,每次只从本地磁盘读取一部分图片内容,加载到内存中,并将这一部分内容写入到目录下,写完以后,内存清空;下一次再从本地磁盘读取一部分数据放入内存。就是为了节省内存空间。 for file in pic.chunks(): f.write(file) else: context = {"info":"图片格式有误!"} return render(request, 'myadmin/info.html', context) goods.save() context = {"info":"添加成功!"} except Exception as e: 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 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: 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)