def addcart(request): goodsid = request.GET.get('goodsid') token = request.session.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 = 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 addtocart(request): # 判断用户是否登录 username = request.session.get('username') # 用户未登录 if username == None: # 跳转到登录页面 return redirect(reverse('axf:login')) # 用户已登录,数据添加到登录车 goods_id = request.GET.get('goodsid') # 通过ID将商品添加到商品的的表里 goods = Goods.objects.get(pk=goods_id) # 获取用户登录信息 user = User.objects.get(u_name=username) # 购物车数量增加 c = Cart.objects.filter(c_user=user).filter(c_goods=goods).filter( c_belong=False) if len(c) == 0: c = Cart() else: c = c.first() num = c.c_goods_num c.c_goods_num = num + 1 # 存储购物信息 c.c_user = user c.c_goods = goods c.save() return JsonResponse({'msg': '添加成功'})
def addcart(request): token = request.session.get('token', '') response_data = {} if token: goodid = request.GET.get('goodid') good = Goods.objects.get(pk=goodid) response_data['statue'] = 1 user = User.objects.get(token=token) carts = Cart.objects.filter(user=user).filter(goods=good) if carts.exists(): cart = carts.first() cart.number += 1 cart.save() else: cart = Cart() cart.user = user cart.goods = good cart.number = 1 cart.save() response_data['statue'] = 1 response_data['number'] = cart.number response_data['msg'] = '添加{}商品成功到购物车{}'.format( cart.goods.productlongname, cart.number) return JsonResponse(response_data) else: response_data['statue'] = -1 return JsonResponse(response_data)
def addcart(request): token = request.session.get('token') response_data = {} if token: userid = cache.get(token) if userid: user = User.objects.get(pk=userid) goodsid = request.GET.get('goodsid') 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() else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() response_data['status'] = 1 response_data['number'] = cart.number response_data['msg'] = '添加 {} 购物车成功: {}'.format( cart.goods.productlongname, cart.number) return JsonResponse(response_data) response_data['status'] = -1 response_data['msg'] = '请登录后操作' return JsonResponse(response_data)
def add_to_cart(request): # 判断用户是否登录 username = request.session.get('username') if username == None: return redirect(reverse('axf:login')) # 用户已登录 goods_id = request.GET.get('goods_id') goods = Goods.objects.get(pk=goods_id) # 获取用户 user = User.objects.get(u_name=username) c = Cart.objects.filter(c_user=user).filter(c_goods=goods).filter( c_belong=False) if len(c) == 0: # 存储购物信息 c = Cart() else: c = c.first() c.c_goods_num += 1 c.c_user = user c.c_goods = goods c.save() return JsonResponse({'msg': '添加成功'})
def addcart(request): goodsid = request.GET.get('goodsid') token = request.session.get('token') responseData = { 'msg': '添加购物车成功', 'status': 1 # 1标识添加成功,0标识添加失败,-1标识未登录 } if token: # 登录 [直接操作模型] # 获取用户 user = User.objects.get(token=token) print('#######################') # 获取商品 goods = Goods.objects.get(pk=goodsid) print(goods) # 商品已经在购物车,只修改商品个数 # 商品不存在购物车,新建对象(加入一条新的数据) carts = Cart.objects.filter(user=user).filter(goods=goods) print(carts) if carts.exists(): # 修改数量 cart = carts.first() print(cart) 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: # 未登录 [跳转到登录页面] # 由于addcart这个是 用于 ajax操作, 所以这里是不能进行重定向!! # return redirect('axf:login') responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): token = request.session.get('token', '') userid = cache.get(token) # print(userid) #获取登录用户信息userid if userid: #true是已经登录状态 user = User.objects.get(pk=userid) goodsid = request.GET.get('goodsid') # print('收到ajax数据产品id',goodsid) goods = Goods.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.goodsnumber += 1 cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.goodsnumber = 1 cart.save() response_data = { 'status': 1, 'mig': '添加{}成功添加总数量{}'.format(cart.goods.productlongname, cart.goodsnumber), 'goodsnumber': cart.goodsnumber } return JsonResponse(response_data) response_data = {'status': 0, 'msg': '请先进行登录'} return JsonResponse(response_data)