def sub_to_cart(request): goodsid = request.GET.get('goodsid') # user_id = request.GET.get('user_id') # if user_id: # return HttpResponse("Add Success") # else: # # data = { # 'status': 302, # 'msg': 'not login' # } # # return JsonResponse(data) # print(Cart.c_user) # print(Cart.c_goods_id) # print(request.user) # print(goodsid) carts=Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num=cart_obj.c_goods_num-1 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() data = { 'status': 200, 'msg': 'sub success', 'c_goods_num': cart_obj.c_goods_num } return JsonResponse(data=data)
def add_to_cart(request): # 获取前端传来的商品id goodsid = request.GET.get('goodsid') # 去购物车表里查看请求的用户 在筛选出当前添加的goodsid carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) # print(carts) # print(type(carts)) # print(carts.values()) # 判断上一步筛选出来是否有数据 如果有取出来(上一步的筛选只能筛出一条数据) 并且将数量加1 if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 # 如果筛选出来没有数据 则把这条数据添加到购物车表 因为模型中c_goods_num默认是1 c_is_select默认是选中 所以这里存的时候不写也可以 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() # print(request.user) data = { 'status': 200, 'msg': 'add success', 'c_goods_num': cart_obj.c_goods_num } return JsonResponse(data=data)
def add_to_cart(request): goodsid = request.GET.get('goodsid') user_id = request.user.id carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() print(request.user) data = { 'status': 200, 'mgs': 'add success', 'c_goods_num': cart_obj.c_goods_num, 'total_price': get_total_price(user_id), } return JsonResponse(data=data)
def add_to_cart(request): goodsid = request.GET.get('goodsid') carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 else: cart_obj = Cart() cart_obj.c_user = request.user cart_obj.c_goods_id = goodsid cart_obj.save() data = { "status": 200, "msg": "add success", "c_goods_num": cart_obj.c_goods_num } return JsonResponse(data=data)
def add_to_cart(request): goodsid = request.GET.get('goodsid') user = request.user carts = Cart.objects.filter(c_good__productid=goodsid).filter(c_user=user) data = { 'status': 700, 'num': 1, } if carts.exists(): cart = carts.first() cart.c_goods_num = cart.c_goods_num + 1 cart.save() data['num'] = cart.c_goods_num else: cart = Cart() cart.c_good = Goods.objects.get(productid=goodsid) cart.c_user = user cart.save() return JsonResponse(data)
def sub_to_cart(request): goodsid = request.GET.get('goodsid') carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() if cart_obj.c_goods_num > 0: cart_obj.c_goods_num = cart_obj.c_goods_num - 1 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() data = { 'status': 200, 'msg': 'sub_success', 'c_goods_num': cart_obj.c_goods_num } return JsonResponse(data=data)
def add_to_cart(request): goodsid=request.GET.get('goodsid') user=request.user carts=Cart.objects.filter(c_goods_id=goodsid).filter(c_user=request.user) if carts: cart=carts.first() cart.c_goods_num=cart.c_goods_num+1 else: cart=Cart() cart.c_user=request.user # good=Goods.objects.get(goodsid=goodsid) # cart.c_goods=good cart.c_goods_id=goodsid cart.c_goods_num=1 cart.save() data={ 'status':200, 'msg':'添加成功', 'c_good_num':cart.c_goods_num, 'total_price': get_total_price(request.user), } return JsonResponse(data)
def addtocart(request): # from market.js goodsid = request.GET.get('goodsid') # AOP middleware # request.user saved in 'LoginMiddleware' carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 else: cart_obj = Cart() cart_obj.c_user = request.user cart_obj.c_goods_id = goodsid cart_obj.save() data = { 'status': 200, 'msg': 'updated cart', 'c_goods_num': cart_obj.c_goods_num, } return JsonResponse(data=data)
def add_to_cart(request): user = request.user good_id = request.POST.get('g_id') good = Goods.objects.get(id=good_id) opreate_type = request.POST.get('opreate_type') carts = Cart.objects.filter(c_user=user).filter(c_goods=good) if carts.exists(): cart = carts.first() if opreate_type == 'add': cart.c_goods_nums = cart.c_goods_nums + 1 elif opreate_type == 'sub': cart.c_goods_nums = cart.c_goods_nums - 1 else: cart = Cart() cart.c_user = user cart.c_goods = good cart.save() data = {'status': 200, 'msg': 'ok', 'num': cart.c_goods_nums} return JsonResponse(data=data)
def get(self, request, *args, **kwargs): action = request.query_params.get("action") data = {} if action == "add_to_cart": goodsid = request.query_params.get("goodsid") carts = Cart.objects.filter(c_user=request.user).filter( c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() data = { 'status': 200, 'msg': 'add success', 'c_goods_num': cart_obj.c_goods_num } elif action == "change_cart_status": cart_id = request.GET.get('cartid') cart_obj = Cart.objects.get(pk=cart_id) cart_obj.c_is_select = not cart_obj.c_is_select cart_obj.save() is_all_select = not Cart.objects.filter( c_user=request.user).filter(c_is_select=False).exists() data = { 'status': 200, 'msg': 'change ok', 'c_is_select': cart_obj.c_is_select, 'is_all_select': is_all_select, 'total_price': get_total_price(self.request) } return Response(data) elif action == "add_shopping": cartid = request.GET.get("cartid") cart_obj = Cart.objects.get(pk=cartid) data = { 'status': 200, 'msg': 'ok', } cart_obj.c_goods_num = cart_obj.c_goods_num + 1 cart_obj.save() data['c_goods_num'] = cart_obj.c_goods_num data['total_price'] = get_total_price(self.request) return Response(data) elif action == "sub_shopping": cartid = request.GET.get("cartid") cart_obj = Cart.objects.get(pk=cartid) data = { 'status': 200, 'msg': 'ok', } if cart_obj.c_goods_num > 1: cart_obj.c_goods_num = cart_obj.c_goods_num - 1 cart_obj.save() data['c_goods_num'] = cart_obj.c_goods_num else: cart_obj.delete() data['c_goods_num'] = 0 data['total_price'] = get_total_price(self.request) return Response(data) elif action == "all_select": cart_list = request.GET.get('cart_list') cart_list = cart_list.split("#") carts = Cart.objects.filter(id__in=cart_list) for cart_obj in carts: cart_obj.c_is_select = not cart_obj.c_is_select cart_obj.save() data = { 'status': 200, 'msg': 'ok', 'total_price': get_total_price(self.request) } elif action == "all": carts = Cart.objects.filter(c_user=request.user) is_all_select = not carts.filter(c_is_select=False).exists() data = { 'status': 200, 'msg': 'ok', "data": { 'title': '购物车', 'is_all_select': is_all_select, 'total_price': get_total_price(self.request), 'carts': CartSerializer(carts, many=True).data, } } return Response(data) else: data["status"] = status.HTTP_400_BAD_REQUEST data["msg"] = "错误的参数" return Response(data)