def addcar(request): token = request.session.get('token') goodid = request.GET.get('goodid') number = request.GET.get('number') print(goodid) if token: user = User.objects.get(token=token) good = Goods.objects.get(pk=goodid) # cart = Car() # cart.user=user # cart.good=good # cart.number=int(number) # cart.save() carts = Car.objects.filter(user=user).filter(good=good) if carts.exists(): # 存在 cart = carts.first() cart.number = cart.number + int(number) cart.save() else: # 不存在 cart = Car() cart.user = user cart.good = good cart.number = int(number) cart.save() return JsonResponse({'status': 1}) else: data = {} data['msg'] = '请登录后操作!' data['status'] = -1 return JsonResponse(data)
def addcart(request): # 拿到token token = request.session.get('token') # 根据token在缓存中找到对应用户 if token: #获取对应用户 userid = cache.get(token) user = User.objects.get(pk=userid) #获取对应商品信息 goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) # 获取商品数量 numb = request.GET.get('numb') # 获取选择的是购物车还是立即购买 shopway = request.GET.get('shopway') #判断商品是否已经存在购物车中 carts = Car.objects.filter(user=user).filter(goods=goods) if carts.exists(): if shopway: cart = carts.first() cart.number = cart.number + int(numb) cart.isdelete = False cart.save() else: cart = Car() cart.user = user cart.goods = goods cart.number = numb cart.save() response_data = { 'status':1, 'msg':'添加购物车成功' } return JsonResponse(response_data) else: response_data = { 'status': -1, 'msg': '请登录再操作' } return JsonResponse(response_data)