Exemple #1
0
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})
Exemple #2
0
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)
Exemple #3
0
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
                  })