def addcart(request): token = request.session.get('token') bookid = request.GET.get('bookid') number = int(request.GET.get('number')) #ajax傳遞數據 data = {} if token: user = User.objects.get(token=token) book = Book.objects.get(pk=bookid) carts = Cart.objects.filter(user=user).filter(book=book) # 進行邏輯判斷 if carts.exists(): cart = carts.first() cart.number = cart.number + number cart.price = cart.number * float(cart.book.newprice) cart.save() else: cart = Cart() cart.user = user cart.book = book cart.number = number cart.price = cart.number * float(cart.book.newprice) cart.save() data['status'] = 1 return JsonResponse(data) else: data['status'] = -1 data['msg'] = "請登錄後操作" return JsonResponse(data)
def addtocart(request): token = request.session.get('token') goodsid = request.GET.get('goodsid') response_data = {} if token: user = User.objects.get(token=token) goods = Goods.objects.get(pk=goodsid) carts = Cart.objects.filter(goods=goods).filter(user=user) if carts.exists(): cart = carts.first() cart.number = cart.number + 1 cart.save() response_data['msg'] = '添加购物车成功' response_data['status'] = 1 response_data['number'] = cart.number return JsonResponse(response_data) else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() response_data['mag'] = '添加购物车成功' response_data['status'] = 1 response_data['number'] = cart.number return JsonResponse(response_data) else: response_data['msg'] = '你还未登录,请登陆' response_data['status'] = '-1' return JsonResponse(response_data)
def addcart(request): goodsid = request.GET.get('goodsid') num = request.GET.get('num') # print(goodsid,num) token = request.COOKIES.get('token') responseData = { 'msg': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登陆 } if token: # 登陆[直接操作模型] # 获取用户 user = User.objects.get(token=token) # 获取商品 goods = Goods.objects.get(pk=goodsid) # 商品已经在购物车,只改商品个数 # 商品不存在购物车,新建对象(加入一条新的数据) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): # 修改数量 cart = carts.first() cart.number = int(cart.number) + int(num) cart.save() responseData['number'] = cart.number else: # 添加一条新记录 cart = Cart() cart.user = user cart.goods = goods cart.number = num cart.save() responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录[跳转到登陆页面] responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): num = request.GET.get('number') goodsid = request.GET.get('goodsid') goodssize = request.GET.get('goodssize') email = request.COOKIES.get('email') print(num) print(goodssize) responseData = { 'msg': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登录 } if email: # 如果用户存在 user = User.objects.get(email=email) goods = Hanfengshishang.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) # 如果购物车有商品,原有的基础上加 if carts.exists(): cart = carts.first() cart.number = cart.number + int(num) cart.size = goodssize cart.save() responseData['number'] = cart.number else: # 如果没有,商品数量等于添加的数量 cart = Cart() cart.user = user cart.goods = goods cart.number = num cart.size = goodssize cart.save() responseData['number'] = cart.number return JsonResponse(responseData)
def addcart(request): goodsid = request.GET.get('goodsid') token = request.session.get('token') responseData = {'msg': '登录购物车成功', 'status': 1} if token: user = User.objects.get(token=token) goods = Goods.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.number = cart.number + 1 cart.save() responseData['number'] = cart.number else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseData['number'] = cart.number return JsonResponse(responseData) else: responseData['msg'] = '未登录,清东路后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): goodsid = request.GET.get('goodsid') # print(goodsid) tel = request.COOKIES.get('tel') responseData={ 'msg':'添加购物车成功', 'status': 1 } if tel: # 获取用户信息 user = User.objects.get(tel=tel) # 获取商品信息 goods = Goodsdetail.objects.get(goodsid=goodsid) # 商品已经在购物车,只修改商品个数 # 商品不存在购物车,新建对象(加入一条新的数据) carts = Cart.objects.filter(user=user).filter(goods=goods) print(carts) if carts.exists(): # 修改数量 cart = carts.first() cart.number = cart.number + 1 cart.save() responseData['number'] = cart.number else: # 添加一条新购物车记录 cart=Cart() cart.user= user cart.goods = goods cart.number = 1 cart.save() responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录 responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): goodsid = request.GET.get('goodsid') # print(goodsid) goodsnum = request.GET.get('goodsnum') goodsnum1 = int(goodsnum) print(type(goodsnum1)) token = request.session.get('token') responseDate = { 'msg': '添加商品到购物车成功', 'status': 1, } if token: user = User.objects.get(token=token) goods = Goods.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.number = cart.number + goodsnum1 cart.save() responseDate['number'] = cart.number else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseDate['number'] = cart.number return JsonResponse(responseDate) else: responseDate['msg'] = '未登录 请登陆后操作' responseDate['status'] = -1 return JsonResponse(responseDate)
def addcart(request): shopid = request.GET.get('shopid') token = request.session.get('token') responseData = { 'msg': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登录 } if token: # 登录 [直接操作模型] # 获取用户 user = User.objects.get(token=token) # 获取商品 shop = Shop.objects.get(id=shopid) # 商品已经在购物车,只修改商品个数 # 商品不存在购物车,新建对象(加入一条新的数据) carts = Cart.objects.filter(user=user).filter(shop=shop) if carts.exists(): # 修改数量 cart = carts.first() number = request.GET.get('number') print(type(number)) cart.isselect = 0 cart.number = str(int(number) + 1) cart.save() responseData['number'] = cart.number else: # 添加一条新记录 cart = Cart() cart.user = user cart.shop = shop cart.number = 1 cart.isselect = 0 cart.save() responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录 [跳转到登录页面] # 由于addcart这个是 用于 ajax操作, 所以这里是不能进行重定向!! responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)