def addcart(request): shoes_id = request.GET.get('shoes_id') num = int(request.GET.get('num')) # 前端传过来的提交数据 print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') print(num) token = request.COOKIES.get('token') resopnse_data = { 'message': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登录 } if token: shoes = Sport_f1_shoes.objects.get(pk=int(shoes_id)) user = User.objects.get(token=token) carts = Cart.objects.filter(user=user).filter(shoes=shoes) if carts.exists(): # 修改数量 cart = carts.first() cart.num += num cart.save() resopnse_data['num'] = cart.num else: # 添加一条新记录 cart = Cart() cart.user = user cart.shoes = shoes # cart.num = 1 cart.num = num cart.save() resopnse_data['num'] = cart.num return JsonResponse(resopnse_data) else: resopnse_data['message'] = '未登录,请登录后操作' resopnse_data['status'] = 0 return JsonResponse(resopnse_data)
def addgoods(request): goods_id = request.GET.get('goods_id') goods = Goods.objects.get(pk=goods_id) # 根据商品ID 获取到对应的商品信息 token = request.session.get('token') responese_data = { 'message': '增加成功', 'status': 1, } if token: user = User.objects.get(token=token) # 筛选得到用户集合并取出第一个用户 carts = Cart.objects.filter(user=user).filter( goods=goods) # 通过商品和用户筛选出对应购物车中的商品 if carts: cart = carts.first() cart.num += 1 cart.save() print(cart) responese_data['num'] = cart.num else: cart = Cart() cart.num = 1 cart.goods = goods cart.user = user cart.save() responese_data['num'] = cart.num return JsonResponse(responese_data) else: responese_data['message'] = '未登陆' responese_data['status'] = 0 return JsonResponse(responese_data)