def goods_add(request): postData = request.POST # post数据 if request.method == "POST" and request.POST: goods_id = postData.get("goods_id") #商品编号 goods_name = postData.get("goods_name") #商品名称 goods_price = postData.get("goods_price") #商品原价 goods_now_price = postData.get("goods_now_price") #商品现价 goods_num = postData.get("goods_num") #商品库存 goods_description = postData.get("goods_description") #商品介绍 goods_content = postData.get("goods_content") #商品详情 goods_show_time = datetime.datetime.now() # 发布时间 types = postData.get("goods_type") #商品类型 # 存入数据库 t = Goods() t.goods_id = goods_id t.goods_name = goods_name t.goods_price = goods_price t.goods_now_price = goods_now_price t.goods_num = goods_num t.goods_description = goods_description t.goods_content = goods_content t.goods_show_time = goods_show_time t.types = Types.objects.get(id=int(types)) t.goods_content = goods_content id = request.COOKIES.get("id") #登录时创建的cookis if id: #防非浏览器访问的爬虫 t.seller = Seller.objects.get(id=int(id)) else: return HttpResponseRedirect("/seller/login/") t.save() imgs = request.FILES.getlist("userfiles") # 保存图片 for index, img in enumerate(imgs): #枚举 # 保存图片到服务器 file_name = img.name file_path = "seller/images/%s_%s.%s" % ( goods_name, index, file_name.rsplit(".", 1)[1]) save_path = os.path.join(MEDIA_ROOT, file_path).replace("/", "\\") try: with open(save_path, "wb") as f: for chunk in img.chunks(chunk_size=1024): f.write(chunk) # 保存路径到数据库 i = Image() i.img_adress = file_path i.img_label = "%s_%s" % (index, goods_name) i.img_description = "this is description" i.goods = t i.save() except Exception as e: print(e) return render(request, "seller/goods_add.html", locals())
def goods_add(request): doType = "" types = Types.objects.all() if request.method == "POST" and request.POST: #获取前端表单数据 postData = request.POST goods_id = postData.get("goods_num") goods_name = postData.get("goods_name") goods_price = postData.get("goods_oprice") # 原价 goods_now_price = postData.get("goods_xprice") # 当前价格 goods_num = postData.get("goods_count") # 库存 goods_description = postData.get("goods_description") # 描述 goods_content = postData.get("goods_content") # 详情 types = postData.get("goods_type") goods_show_time = datetime.datetime.now() # 发布时间 #存入数据库 #先保存商品 goods = Goods() goods.goods_id = goodsNum() goods.goods_name = goods_name goods.goods_price = goods_price goods.goods_now_price = goods_now_price goods.goods_num = goods_num goods.goods_description = goods_description goods.goods_content = goods_content goods.goods_show_time = goods_show_time goods.types = Types.objects.get(id=int(types)) id = request.COOKIES.get("id") if id: goods.seller = Seller.objects.get(id=int(id)) else: return HttpResponseRedirect("/seller/login/") goods.save() imgs = request.FILES.getlist("userfiles") #保存图片 for index, img in enumerate(imgs): #保存图片到服务器 file_name = img.name file_path = "seller/images/%s_%s.%s" % ( goods_name, index, file_name.rsplit(".", 1)[1]) save_path = os.path.join(MEDIA_ROOT, file_path).replace("/", "\\") try: with open(save_path, "wb") as f: for chunk in img.chunks(chunk_size=1024): f.write(chunk) #保存路径到数据库 i = Image() i.img_adress = file_path i.img_label = "%s_%s" % (index, goods_name) i.img_description = "this is description" i.goods = goods i.save() except Exception as e: print(e) return render(request, "seller/goods_add.html", locals())