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 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)