def add(request, gid, count): if int(gid) == 0 and request.is_ajax() and int(count) == 0: count = CartInfo.objects.filter( user_id=request.session['user_id']).count() return JsonResponse({'count': count}) # 获取当前登录用户的ID uid = request.session['user_id'] gid = int(gid) count = int(count) # 查询购物车是否已有该商品,如果有这增加其数量,没有则增加一个新的商品 carts = CartInfo.objects.filter(user_id=uid, goods_id=gid) if len(carts) >= 1: cart = carts[0] cart.count = cart.count + count else: cart = CartInfo() cart.user_id = uid cart.goods_id = gid cart.count = count cart.save() # 如果是ajax请求,则返回json,否则转向购物车 if request.is_ajax(): count = CartInfo.objects.filter( user_id=request.session['user_id']).count() # 插查询当前登录用户购物车的商品类型总数量 return JsonResponse({'count': count}) else: return redirect('/cart/') # 转向购物车
def order_again(request, pk): orderinfo = OrderInfo.objects.get(oid=pk) uid = request.session['user_id'] for order_detail in orderinfo.orderdetailinfo_set.all(): cart = CartInfo() cart.user_id = uid cart.goods_id = order_detail.goods_id cart.count = order_detail.count cart.save() return redirect(reverse('df_cart:cart'))
def add(request, gid, count): #用户uid购买了gid商品,数量为count uid = request.session['user_id'] gid = int(gid) #浏览器传过来的参数是字符串 count = int(count) #查询购物车中是否已经有此商品,如果有则数量增加,如果没有则新增 carts = CartInfo.objects.filter(user_id=uid, goods_id=gid) if len(carts) >= 1: cart = carts[0] cart.count = cart.count + count else: cart = CartInfo() cart = CartInfo() cart.user_id = uid cart.goods_id = gid cart.count = count cart.save() #如果是ajax请求则返回json if request.is_ajax(): count = CartInfo.objects.filter( user_id=request.session['user_id']).count() return JsonResponse({'count': count, 'cart_id': cart.id})
def add(request, goodsId, goodCount): userId = request.session['user_id'] Carts = CartInfo.objects.filter(user_id=userId).filter(goods_id=goodsId) if len(Carts) > 0: cart = Carts[0] cart.count += int(goodCount) else: cart = CartInfo() cart.user_id = userId cart.goods_id = goodsId cart.count = goodCount cart.save() carts = CartInfo.objects.filter(user_id=userId) return render(request, 'df_cart/cart.html', {'carts': carts})
def add(request): uid = request.session.get('user_id') # 用户的id dict1 = request.GET gid = dict1.get('gid') # 商品的id count = int(dict1.get('count')) # 数量 # 判断当前商品是否已经在该用户购物车中 carts = CartInfo.objects.filter(user_id=uid, goods_id=gid) if carts: cart = carts[0] cart.count += count cart.save() else: cart = CartInfo() cart.user_id = uid cart.goods_id = gid cart.count = count cart.save() # 统计当前用户购物车商品数量 c = CartInfo.objects.filter(user_id=uid).aggregate(Sum('count')) return JsonResponse({'ok': 1, 'count': c.get('count__sum')})
def add(request, gid, count): # 用户uid购买商品gid,数量为count uid = request.session['user_id'] gid = int(gid) count = int(count) carts = CartInfo.objects.filter(user_id=uid, goods_id=gid) if len(carts) >= 1: cart = carts[0] cart.count = cart.count + 1 else: cart = CartInfo() cart.user_id = uid cart.goods_id = gid cart.count = count cart.save() if request.is_ajax(): count = carts = CartInfo.objects.filter(user_id=request.session['user_id']).count() return JsonResponse({'count':count}) else: return redirect('/cart/cart/')
def add(request, goodid, goodcount): #先获取用户id userId = request.session['user_id'] #先验证购物车里面是否存在相同的产品 Carts = CartInfo.objects.filter(user_id=userId).filter(goods_id=goodid) if len(Carts) > 0: cart = Carts[0] cart.count += int(goodcount) else: #定义一个购物车 cart = CartInfo() cart.user_id = userId cart.goods_id = goodid cart.count = goodcount #保存购物车信息 cart.save() #根据用户查询他的购物车信息 carts = CartInfo.objects.filter(user_id=userId) return render(request, 'df_cart/cart.html', { 'carts': carts, 'len': len(carts) })
def add(request,gid,count): uid=request.session.get('user_id') gid=int(gid) count=int(count) carts=CartInfo.objects.filter(user_id=uid,goods_id=gid) if len(carts)>=1: cart=carts[0] cart.count=cart.count+count else: cart=CartInfo() cart.user_id=uid cart.goods_id=gid cart.count=count cart.save() count_s=CartInfo.objects.filter(user_id=uid).count() request.session['count']=count_s if request.is_ajax(): print '*'*10 print 'ajax' return JsonResponse({'count':count_s}) else: return redirect('/cart/')
def add(request,goodid,count): #从session中获取用户id uid=request.session.get('user_id') count=int(count)#字符串转整数 #从数据库中根据用户id和商品id查询数量 cartgood_list = CartInfo.objects.filter(user_id=uid,goods_id=goodid) # 对象列表cartgood_list的长度要么是0 要么是1 if len(cartgood_list):#购物车已有该商品 cartgood=cartgood_list[0] cartgood.count+=count cartgood.save() else:#购物车没有该商品 cartgood =CartInfo() cartgood.user_id=uid cartgood.goods_id=goodid cartgood.count=count cartgood.save() #通过json返回购物车中商品的总数量 result=CartInfo.objects.filter(user_id=uid).count() #求数量。相当于len() data={'count':result} return JsonResponse(data)
def deletegood(request, cartId): cart = CartInfo() cart.id = cartId count = CartInfo.delete(cart) return JsonResponse({'date': 'ok'})