def enterOrder(request): buyer_id = request.COOKIES.get("user_id") #用户的id goods_list = [] #订单商品列表 if request.method == "POST" and request.POST: requestData = request.POST #请求数据 addr = requestData.get("address") #寄送的地址 pay_method = requestData.get("pay_method") #获取商品信息 all_price = 0 #总价 for key, value in requestData.items(): #循环所有的数据 if key.startswith("name"): #如果键以name开头,我们的任务是一条商品信息的id buyCar = BuyCar.objects.get(id=int(value)) #获取商品 good = Goods.objects.get(id=buyCar.goods_id) good_img = good.image_set.first().img_adress.url.replace( "media", "static") price = float(buyCar.goods_num) * float( buyCar.goods_price) #单条商品的总价 all_price += price #计算总价 goods_list.append({ "price": price, "buyCar": buyCar, "img": good_img }) #构建数据模型(小计总价:price,商品信息:buyCar) #存入订单库 Addr = Address.objects.get(id=int(addr)) #获取地址数据 order = Order() #保存到订单 #订单编号 日期 +随机 +订单id now = datetime.datetime.now() order.order_num = now.strftime("%Y%m%d") + str( random.randint(10000, 99999)) order.order_time = now #状态 未支付 1 支付成功 2 配送中 3 交易完成 4 已取消0 order.order_statue = 1 order.total = all_price order.user = Buyer.objects.get(id=int(buyer_id)) order.order_address = Addr order.save() order.order_num = order.order_num + str(order.id) order.save() for good in goods_list: #循环保存到订单当中的商品 g = good["buyCar"] g_o = OrderGoods() g_o.goods_id = g.id g_o.goods_name = g.goods_name g_o.goods_price = g.goods_price g_o.goods_num = g.goods_num g_o.goods_picture = g.goods_picture g_o.order = order g_o.save() return render(request, 'buyer/enterOrder.html', locals()) else: return HttpResponseRedirect("/buyer/carList")
def add_order(request): buyer_id = request.COOKIES.get("user_id") goods_list = [] if request.method == "POST" and request.POST: requestDate = request.POST addr = requestDate.get("address") #寄送地址的id pay_method = requestDate.get("pay_Method") all_price = 0 #总价 for key, value in requestDate.items(): #遍历出所有输出 if key.startswith("name"): #判断是不是商品信息id buyCar = BuyCar.objects.get(id=int(value)) g = Goods.objects.get(id=buyCar.goods_id) img = g.image_set.all().first().img_adress.url.replace( "media", "static") price = float(buyCar.goods_num) * float(buyCar.goods_price) all_price += price goods_list.append({ "price": price, "buyCar": buyCar, "img": img }) Addr = Address.objects.get(id=int(addr)) order = Order() #保存到订单 now = datetime.datetime.now() # 订单编号 日期 + 随机 + 订单 + id nowdata = now.strftime("%Y-%m-%d") order.order_num = now.strftime("%Y-%m-%d") + "-" + str( random.randint(10000, 99999)) + str(order.id) order.order_time = now order.order_statue = 1 # 状态 未支付 1 支付成功 2 配送中 3 交易完成 4 已取消 0 order.total = all_price order.user = Buyer.objects.get(id=int(buyer_id)) order.order_address = Addr order.save() for good in goods_list: # 循环保存订单当中的商品 g = good["buyCar"] print(g.id) g_o = OrderGoods() g_o.goods_id = g.id g_o.goods_name = g.goods_name g_o.goods_price = g.goods_price g_o.goods_num = g.goods_num g_o.goods_picture = g.goods_picture g_o.order = order g_o.save() return render(request, "buyer/enterOrder.html", locals()) else: return HttpResponseRedirect("/buyer/carList/")
def add_order(request): buyer_id = request.COOKIES.get("user_id") #用户的id goods_list = [] #订单商品的列表 if request.method == "POST" and request.POST: requestData = request.POST #请求数据 addr = requestData.get("address") #寄送地址的id pay_method = requestData.get("pay_Method") #支付方式 #获取商品信息 all_price = 0 #总价 for key, value in requestData.items(): #循环所有的数据 if key.startswith("name"): #如果键以name开头,我们就任务是一条商品信息的id buyCar = BuyCar.objects.get(id=int(value)) #获取商品 price = float(buyCar.goods_num) * float( buyCar.goods_price) #单条商品的总价 all_price += price #加入总价 goods_list.append({ "price": price, "buyCar": buyCar }) #构建数据模型{"小计总价":price,"商品信息":buyCar} # 存入订单库 Addr = Address.objects.get(id=int(addr)) #获取地址数据 order = Order() #保存到订单 now = datetime.datetime.now() #订单编号 日期 + 随机 + 订单 + id order.order_num = now.strftime("%Y-%m-%d") + str( random.randint(10000, 99999)) + str(order.id) order.order_time = now # 状态 未支付 1 支付成功 2 配送中 3 交易完成 4 已取消 0 order.order_statue = 1 order.total = all_price order.user = Buyer.objects.get(id=int(buyer_id)) order.order_address = Addr order.save() for good in goods_list: #循环保存订单当中的商品 g = good["buyCar"] g_o = OrderGoods() g_o.goods_id = g.id g_o.goods_name = g.goods_name g_o.goods_price = g.goods_price g_o.goods_num = g.goods_num g_o.goods_picture = g.goods_picture g_o.order = order g_o.save() return render(request, "buyer/enterOrder.html", locals()) else: return HttpResponseRedirect("/buyer/carList/")