def generateorder(request): token = request.session.get('token') user = User.objects.get(token=token) # 生成订单 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 generateorder(request): #确定用户身份 token = request.session.get('token') userid = cache.get(token) # print(userid) user = User.objects.get(pk=userid) # print(user) #生成订单 order = Order() order.user = user #表明订单用户 属于谁 order.identifier = generate_identifier() #生成订单号 order.save() #订单中的商品 从购物车中循环拿出 carts = user.cart_set.filter(isselect=True) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.goodsnumber orderGoods.save() #加入订单的商品从购物车中移除 cart.delete() return render(request, 'order/orderdetail.html', context={'order': order})
def generateorder(request): token = request.session.get('token') user = User.objects.get(token=token) carts = user.cart_set.filter(isselect=True) if carts.first() == None: return redirect('axf:cart') # 订单 order = Order() order.user = user order.identifier = generate_identifier() order.save() # 订单商品(购物车中选中) for cart in carts: orderGoods = OrderGoods() orderGoods.order = order orderGoods.goods = cart.goods orderGoods.number = cart.number orderGoods.save() # 购物车中移除 cart.delete() # response_data = { # 'msg': '生成订单', # 'status': 1, # 'identifier': order.identifier # } # # return JsonResponse(response_data) sum = 0 for orderGoods in order.ordergoods_set.all(): sum += orderGoods.goods.price * orderGoods.number return render(request, 'order/orderdetail.html', context={ 'order': order, 'sum': sum })
def gen_order(request): cartids = request.GET.get('cartids') cartids = cartids.split('=') print(cartids) order = Order() username = request.session.get('username') user = User.objects.get(u_name=username) order.o_user = user order.o_status = 1 order.save() # 修改商品属于哪张表 for cartid in cartids: car = Cart.objects.get(pk=cartid) car.c_belong = True car.c_order = order car.save() return JsonResponse({'msg': 'ok', 'orderid': order.id})
def genorder(request): cartids = request.GET.get('cartids') # 列表拆分 cartids = cartids.split('#') # print(cartids) # 生成订单 将要购买的商品转换到订单表中,生成订单信息,将购买的商品的订单信息关联 order = Order() username = request.session.get('username') user = User.objects.get(u_name=username) # order.o_user = user # 定义状态 0,默认生成状态 , 1 ,已下单未付款 , 2 已付款 ,3 已付款并已发货.... order.o_status = 1 order.save() # order 在存储之后就有id了 for item in cartids: car = Cart.objects.get(pk=item) # 修改属于那张表 car.c_belong = True car.c_order = order car.save() return JsonResponse({'msg': 'ok', 'orderid': order.id})