def generateorder(request): tel= request.COOKIES.get('tel') user = User.objects.get(tel=tel) # 生成订单 order = Order() order.user = user order.identifier = str(int(time.time())) + str(random.randrange(10000,100000)) order.save() # 订单商品 carts = Cart.objects.filter(user=user).filter(isselect=True) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.number orderGoods.save() cart.delete() responseData = { 'msg': '订单生成成功', 'status': 1, 'identifier': order.identifier } return JsonResponse(responseData)
def genetateorder(request): token = request.session.get("token") user = User.objects.get(token=token) carts = Cart.objects.filter(user=user).filter(isselect=True) if carts.exists(): # 生成訂單 order = Order() order.user = user order.identifier = generate_identifier() order.save() # 訂單商品 # 獲取用戶的購物車 for cart in carts: orderbook = Orderbook() orderbook.order = order orderbook.book = cart.book orderbook.number = cart.number orderbook.price = cart.price orderbook.save() # 從購物車中刪除,數據存入了orderbook cart.delete() data = {"msg": "下單成功", "status": 1, "identifier": order.identifier} return JsonResponse(data) else: data = { "msg": "下單失敗,請先選中商品", "status": -1, } return JsonResponse(data)