def insert(request): '''执行添加操作''' goods = Goods() goods.goods = request.POST['goods'] #商品名称 goods.typeid = request.POST['typeid'] goods.company = request.POST['company'] #生产厂家 goods.price = request.POST['price'] #价格 goods.store = request.POST['store'] #库存 goods.state = 1 #状态 goods.content = request.POST['content'] #商品简介 goods.addtime = datetime.now().strftime("%Y-%m-%d %H:%H:%S") photofile = request.FILES.get('file', None) if photofile == None: context = {'info': '未获取到图片信息!请重新添加。'} return render(request, './myadmin/goods/info.html', context) goods.picname = str(time.time()) + '.' + photofile.name.split('.').pop() openphoto = open('./static/myadmin/photo/' + goods.picname, 'wb+') for chunk in photofile.chunks(): openphoto.write(chunk) openphoto.close() # 执行图片缩放 im = Image.open("./static/myadmin/photo/" + goods.picname) # 缩放到75*75(缩放后的宽高比例不变): im.thumbnail((100, 100)) im.save("./static/myadmin/photo/s_" + goods.picname, None) #执行图片删除 os.remove("./static/myadmin/photo/" + goods.picname) goods.save() context = {'info': '商品信息添加成功!'} return render(request, './myadmin/goods/info.html', context)
def insert(request): try: myajax_content = request.POST.get('content') # 判断是否有圖,并执行图片上传,缩放等处理 myfile = request.FILES.get("pic", None) if not myfile: return HttpResponse("尚未上传雜誌封面圖!") # 以时间戳命名一个新图片名称 filename = str(int(time.time())) + "_.jpg" 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/" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((220, 220)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/m_" + filename, 'jpeg') # 缩放到75*75: im.thumbnail((75, 75)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/s_" + filename, 'jpeg') # 获取商品信息并执行添加 ob = Goods() ob.goods = request.POST['goods'] ob.typeid = request.POST['typeid'] ob.publish_date = request.POST['publish_date'] ob.language = request.POST['language'] ob.price = request.POST['price'] ob.store = request.POST['store'] ob.content = html.unescape( myajax_content) # html.unescape 可以與前端的模板語法safe相呼應,避免亂碼 ob.picname = filename ob.state = 1 # ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") ob.addtime = timezone.now() ob.save() context = {'info': '添加成功!'} except Exception as err: print(err) context = {'info': '添加失败!'} return render(request, "myadmin/info.html", context)
def 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+') for chunk in myfile.chunks(): # 分块写入文件 destination.write(chunk) destination.close() # 执行图片缩放 im = Image.open("./static/goods/" + filename) # 2880*480 首页长图 im.thumbnail((2880, 480)) im.save("./static/goods/l_" + filename, 'jpeg') # 缩放到375*375: im.thumbnail((375, 375)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/" + filename, 'jpeg') # 缩放到220*220: im.thumbnail((220, 220)) # 把缩放后的图像用jpeg格式保存: im.save("./static/goods/m_" + filename, 'jpeg') # 缩放到75*75: im.thumbnail((75, 75)) # 把缩放后的图像用jpeg格式保存: im.save("./static/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.content = request.POST['content'] ob.picname = filename ob.state = 1 ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") ob.save() context = {'info': '添加成功!'} except Exception as err: print(err) context = {'info': '添加失败!'} return render(request, "myadmin/info.html", context)
def insert(request): '''执行添加''' try: # 图片的上传处理 myfile = request.FILES.get("pic",None) if not myfile: return HttpResponse("没有上传文件信息") filename = str(time.time())+"."+myfile.name.split('.').pop() destination = open("./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)) im.save("./static/goods/"+filename,None) im = Image.open("./static/goods/"+filename) # 缩放到220*220(缩放后的宽高比例不变): im.thumbnail((220,220)) im.save("./static/goods/m_"+filename,None) im = Image.open("./static/goods/"+filename) # 缩放到75*75(缩放后的宽高比例不变): im.thumbnail((75, 75)) im.save("./static/goods/s_"+filename,None) #保存商品信息 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.content = request.POST['content'] ob.picname = filename ob.state = 1 ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") ob.save() context={"info":"添加成功!"} except Exception as err: print(err) context={"info":"添加失败"} return render(request,"myadmin/info.html",context)
def 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+') for chunk in myfile.chunks(): destination.write(chunk) destination.close() im = Image.open("./static/goods/" + filename) # rgb_im = im.convert('RGB') im.thumbnail((375, 375)) im.save("./static/goods/" + filename) im.thumbnail((220, 220)) im.save("./static/goods/m_" + filename) im.thumbnail((75, 75)) im.save("./static/goods/s_" + filename) 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.content = request.POST['content'] ob.picname = filename ob.state = 1 ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") ob.save() context = {'info': '添加成功!'} except Exception as err: print(err) context = {'info': '添加失败!'} return render(request, "admin/info.html", context)
def insert(request): '''执行添加''' try: #保存商品信息 ob = Goods() if request.POST['goods']=='': context={"info":"商品名称不能为空!"} request.session['url']='myadmin_goods_add' request.session['data']=-1 return render(request,"myadmin/info.html",context) else: ob.goods = request.POST['goods'] if request.POST['price']=='': context={"info":"单价不能为空!"} request.session['url']='myadmin_goods_add' request.session['data']=-1 return render(request,"myadmin/info.html",context) else: ob.price = request.POST['price'] if request.POST['store']=='': request.session['url']='myadmin_goods_add' request.session['data']=-1 context={"info":"库存量不能为空!"} return render(request,"myadmin/info.html",context) else: ob.store = request.POST['store'] #图片的上传处理 myfile = request.FILES.get("pic",None) if not myfile: context={"info":"没有上传文件!"} request.session['url']='myadmin_goods_add' request.session['data']=-1 return render(request,"myadmin/info.html",context) filename = str(time.time())+"."+myfile.name.split('.').pop() destination = open("./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)) im.save("./static/goods/"+filename,None) im = Image.open("./static/goods/"+filename) #缩放到220*220(缩放后的宽高比例不变): im.thumbnail((220,220)) im.save("./static/goods/m_"+filename,None) im = Image.open("./static/goods/"+filename) #缩放到75*75(缩放后的宽高比例不变): im.thumbnail((75, 75)) im.save("./static/goods/s_"+filename,None) #保存商品信息 ob.typeid = request.POST['typeid'] ob.company = request.POST['company'] ob.content = request.POST['content'] ob.picname = filename ob.state = 0 ob.addtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") ob.save() context={"info":"添加成功!"} request.session['url']='myadmin_goods_index' request.session['data']=-1 except Exception as err: print(err) request.session['url']='myadmin_goods_add' request.session['data']=-1 context={"info":"添加失败!"} return render(request,"myadmin/info.html",context)