def myhome_order_create(request): # 接收数据 data = request.GET.dict() # 创建订单 ob = Order() ob.uid = Users.objects.get(id=request.session.get('VipUser')['uid']) ob.username = data['name'] ob.phone = data['phone'] ob.address = data['address'] ob.totalprice = 0 ob.save() print(data) # 创建订单详情 cartdata = Cart.objects.filter(id__in=data['cartids'].split(',')) totalprice = 0 for x in cartdata: obinfo = OrderInfo() obinfo.orderid = ob obinfo.goodsid = x.goodsid obinfo.num = x.num obinfo.price = x.goodsid.price obinfo.save() # 计算总价 totalprice += x.num * x.goodsid.price # 删除购物车中已下单的商品 x.delete() # 修改订单总价 ob.totalprice = totalprice ob.save() did = str(ob.id) return JsonResponse({'code': 0, 'msg': '订单创建成功,请支付', 'oid': did})
def ordercreate(request): #在session获取订单数据 data = request.session['order'] print(data) totalprice = 0 totalnum = 0 for x in data: n = float(data[x]['price']) * data[x]['num'] totalprice += n totalnum += data[x]['num'] #创建订单 order = Order() order.uid = Users.objects.get(id=request.session['VipUser']['uid']) order.address = Address.objects.get(id=request.POST['addressid']) order.totalprice = totalprice order.totalnum = totalnum order.status = 1 order.save() #读取购物车数据 cart = request.session['cart'] #创建订单详情 for x in data: orderinfo = OrderInfo() #订单号 orderinfo.orderid = order #获取当前购买的商品对象 goods = Goods.objects.get(id=x) orderinfo.gid = goods orderinfo.num = data[x]['num'] orderinfo.price = data[x]['price'] orderinfo.save() #创建完订单后 删除购物车session的数据 del cart[x] #修改商品的购买数量 和库存 goods.num += data[x]['num'] goods.storage -= data[x]['num'] goods.save() #清空session中的订单数据 request.session['order'] = {} #更新购物车 request.session['cart'] = cart #跳转到付款页面 后面跟上order 的id号 return HttpResponse( '<script>alert("订单创建成功");location.href="/order/buy/?orderid=' + str(order.id) + '"</script>')
def ordercreate(request): # 接受用户id uid = request.session['VipUser']['uid'] # 接受地址id addressid = request.POST['addressid'] # 商品信息 data = request.session['order'] # 获取购物车中的数据 cart = request.session['cart'] # 生成订单 ob = Orders() ob.uid = Users.objects.get(id=uid) ob.addressid = Address.objects.get(id=addressid) ob.totalprice = data['totalprice'] ob.totalnum = data['totalnum'] ob.save() # 订单详情 for v in data['items']: oinfo = OrderInfo() oinfo.orderid = ob oinfo.gid = Goods.objects.get(id=v['goodsid']) oinfo.num = v['num'] oinfo.save() # 在购物车中删除当前购买的商品 del cart[v['goodsid']] # 清楚购物车中已经下单的商品,清楚order数据 request.session['cart'] = cart request.session['order'] = '' # 把生成订单id 请求到一个新的付款页面 return HttpResponse('<script>location.href="/buy/?orderid=' + str(ob.id) + '"</script>')
def createorder(request): aid = request.POST['addrid'] uid = request.session['User']['uid'] data = request.session['order'] cart = request.session['cart'] ob = Orders() ob.uid = Users.objects.get(id=uid) ob.addressid = Address.objects.get(id=aid) ob.tolprice = data['tolprice'] ob.tolnum = data['tolnum'] ob.save() for i in data['items']: obinfo = OrderInfo() obinfo.orderid = ob obinfo.gid = Goods.objects.get(id=i['gid']) obinfo.num = i['gnum'] obinfo.save() del cart[i['gid']] request.session['count'] -= 1 request.session['cart'] = cart request.session['order'] = '' return HttpResponse('<script>location.href="/pay/?orderid=' + str(ob.id) + '"</script>')
def ordercreate(request): # 接受用户id uid = request.session['VipUser']['uid'] # 收货地址id addressid = request.POST['addressid'] # 商品信息 data = request.session['order'] # 获取购物车中的数据 cart = request.session['cart'] ''' { '4': {'price': 9.9, 'num': 2, 'id': 4, 'title': '小蛋糕真好吃:23', 'pics': '/static/pics/1530080135.535089758178.jpg'}, '22': {'price': 9.9, 'num': 3, 'id': 22, 'title': '小蛋糕真好吃:24', 'pics': '/static/pics/1530080135.535089758178.jpg'}, '3': {'price': 1999.99, 'num': 1, 'id': 3, 'title': '小蛋糕真好吃:22', 'pics': '/static/pics/1530080135.535089758178.jpg'} } ''' # 生成订单 ob = Orders() ob.uid = admin.objects.get(id=uid) ob.addressid = Address.objects.get(id=addressid) ob.totalprice = data['totalprice'] ob.totalnum = data['totalnum'] ob.save() # 'items': [ # {'price': 1999.99, 'pics': '/static/pics/1530080135.535089758178.jpg', 'num': 1, 'title': '小蛋糕真好吃:22', 'goodsid': '3'}, # {'price': 9.9, 'pics': '/static/pics/1530080135.535089758178.jpg', 'num': 3, 'title': '小蛋糕真好吃:24', 'goodsid': '22'} # ] # .订单详情 for v in data['items']: oinfo = OrderInfo() oinfo.orderid = ob oinfo.gid = Goods.objects.get(id=v['goodsid']) oinfo.num = v['num'] oinfo.save() # 在购物车中删除当前购买的商品 del cart[v['goodsid']] # 清除购物车中已经下单的商品,清除order数据 request.session['cart'] = cart request.session['order'] = '' # 把生成订单id get请求到一个新的付款页面 return HttpResponse('<script>location.href="/buy/?orderid='+str(ob.id)+'"</script>')
def ordercreate(request): # 在session中获取商品数据 data = request.session['order'] totalprice = 0 totalnum = 0 for x in data: n = float(data[x]['price']) * data[x]['num'] totalprice += n totalnum += data[x]['num'] # 创建订单 order = Order() order.uid = Users.objects.get(id=request.session['VipUser']['uid']) order.address = Address.objects.get(id=request.POST['addressid']) order.totalprice = totalprice order.totalnum = totalnum order.status = 1 order.save() # 先读取购物车里面的数据 cart = request.session['cart'] # 创建订单详情 for x in data: orderinfo = OrderInfo() orderinfo.orderid = order goods = Goods.objects.get(id=x) orderinfo.gid = goods orderinfo.num = data[x]['num'] orderinfo.price = data[x]['price'] orderinfo.save() # 将购物车中的商品删除 del cart[x] # 修改狗台数据 goods.num += data[x]['num'] goods.storage -= data[x]['num'] goods.save() # 清空session中的订单数据 request.session['order'] = {} # 更新购物车 request.session['cart'] = cart # 跳转到付款页面 return HttpResponse( '<script>alert("订单创建成功,请立即支付");location.href="/order/buy/?orderid=' + str(order.id) + '"</script>')
def ordercreate(request): # 接受ids 已经选择的商品id和数量 ids = eval(request.POST.get('ids')) # 创建订单 ob = Order() ob.uid = Users.objects.get(id=request.session['VipUser']['uid']) ob.aid = Address.objects.get(id=request.POST.get('addressid')) ob.totalprice = 0 ob.save() # 获取session中的购物车数据 CartData = request.session['cart'] # 创建订单详情 totalprice = 0 for k, v in ids.items(): obi = OrderInfo() obi.orderid = ob g = Goods.objects.get(id=k) obi.gid = g obi.num = v obi.price = g.price obi.save() # 把已经下单的商品在购物车中删除 CartData.pop(k) totalprice += g.price * v # 修改总价 ob.totalprice = totalprice ob.save() # 把修改好的购物车数据,重新放入session request.session['cart'] = CartData return HttpResponse('<script>location.href="/order/buy/?orderid=' + str(ob.id) + '"</script>')
def ordercreate(request): # 接收用户id uid = request.session['VipUser']['uid'] # 收货地址id addressid = request.POST.get('addressid') # 商品信息 data = request.session['order'] # 获取购物车中的数据 cart = request.session['cart'] # 生成订单 ob = Orders() ob.uid = Users.objects.get(id=uid) ob.addressid = Address.objects.get(id=addressid) ob.totalprice = float(data['totalprice']) ob.totalnum = data['totalnum'] ob.save() # 订单详情 for v in data['items']: oinfo = OrderInfo() oinfo.orderid = ob oinfo.gid = Goods.objects.get(id=v['goodsid']) oinfo.num = v['num'] oinfo.save() # 在购物车中删除当前的商品 del cart[v['goodsid']] # 清楚购物车中已经下单的商品 清楚order数据 request.session['cart'] = cart request.session['order'] = '' print(ob.id) # 把生成订单id get请求到一个新的付款页面 return HttpResponse( '<script>alert("订单生成成功");location.href="/buy/?orderid=' + str(ob.id) + '"</script>')
def myhome_order_create(request): # 接受数据 data = request.POST.dict() data.pop('csrfmiddlewaretoken') # {'username': '******', 'address': '北京市', 'cartids': '9,10', 'phone': '13701383017'} # 创建订单 ob = Order() ob.uid = Users.objects.get(id=request.session.get('VipUser')['uid']) ob.username = data['username'] ob.phone = data['phone'] ob.address = data['address'] ob.totalprice = 0 ob.save() # 创建订单详情 cartdata = Cart.objects.filter(id__in=data['cartids'].split(',')) totalprice = 0 for x in cartdata: # 创建订单详情 obinfo = OrderInfo() obinfo.orderid = ob obinfo.goodsid = x.goodsid obinfo.num = x.num obinfo.save() # 计算总价 totalprice += x.num * x.goodsid.price # 删除购物车中已经下单的商品 x.delete() # 修改订单的总价 ob.totalprice = totalprice ob.save() # 重定向到支付页面 return HttpResponse( '<script>alert("订单创建成功,请支付");location.href="/order/pay/?orderid=' + str(ob.id) + '"</script>')
def shop_cart_create(request): data = request.POST.dict() data.pop('csrfmiddlewaretoken') # 创建订单 try: ob = Order() ob.uid = Users.objects.get(id=request.session.get('vipUser')['id']) ob.username = data['true_name'] ob.phone = data['mob_phone'] ob.address = data['address'] ob.totalprice = 0 ob.save() except Exception as e: print('创建订单',e) return HttpResponse("<script>alert('信息填写不正确');history.back(-1)</script>") # 创建订单详情 try: cartdata = Cart.objects.filter(id__in = data['cartids0'].split(',')) totalprice = 0 for x in cartdata: obinfo = OrderInfo() obinfo.orderid = ob obinfo.goodsid = x.goodsid obinfo.num = x.num obinfo.save() # 计算总价 totalprice += x.num*x.goodsid.price # 删除购物车中已经下单的商品 x.delete() ob.totalprice = totalprice ob.save() except Exception as e: print('创建订单详情',e) return HttpResponse("<script>alert('订单有误');history.back(-1)</script>") # 重定向到支付页面 return HttpResponse('<script>alert("订单创建成功,请支付");location.href="/cart/pay/?orderid='+str(ob.id)+'"</script>')