def addcart(request): goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) print(goodsid) token = request.session.get('token') userid = cache.get(token) response_data = {} if userid: user = User.objects.get(pk=userid) carts = user.cart_set.filter(goods=goods).filter(user=user) response_data['status'] = 1 if carts: cart = carts.first() print('cart exist') cart.number = cart.number +1 cart.save() response_data['goodsnumber'] = cart.number return JsonResponse(response_data) else: print('cart is not exist') cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() return JsonResponse(response_data) else: response_data['status'] = 0 return JsonResponse(response_data)
def addcart(request): token = request.session.get('token') userid = cache.get(token) goodsid = request.GET.get('goodsid') goodsnum = request.GET.get('goodsnum') # print(goodsid) response_data={ 'user':None, } if userid: user = User.objects.get(pk=userid) goods = Goods.objects.get(pk=goodsid) cart = Cart.objects.filter(goods=goods).filter(user=user) if cart.exists(): cart=cart.first() cart.num = cart.num + int(goodsnum) cart.save() else: cart=Cart() cart.goods=goods cart.user=user cart.num = int(goodsnum) cart.save() # response_data['user'] = user response_data['user_status'] = 1 response_data['cart_num']=cart.num # response_data['goodsid'] = goodsid else: response_data['user_status'] = 0 return JsonResponse(response_data)
def quicklybuy(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') num = int(request.GET.get('number1')) 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 + num cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.number = num cart.save() response_data['status'] = 1 response_data['number'] = cart.number response_data['msg'] = '添加 {} 购物车成功: {}'.format(cart.goods.productname, cart.number) return JsonResponse(response_data) response_data['status'] = -1 response_data['msg'] = '请登录后操作' return JsonResponse(response_data)
def addcart(request): token = request.session.get('token') goodsid = request.GET.get('goodsid') data = {} 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() else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() return JsonResponse({ 'msg': '{},添加购物车成功'.format(goods.productlongname), 'number': cart.number, 'status': 1 }) else: data['msg'] = '请登录后操作!' data['status'] = -1 return JsonResponse(data)
def addcart(request): token = request.session.get('token') response_data = {} if token: userid = cache.get(token) user = User.objects.filter(pk=userid) if user.exists(): user = user.first() goodsid = request.GET.get('goodsid') good = Goods.objects.filter(pk=goodsid).first() cart = Cart.objects.filter(user=user).filter(goods=good) if cart.exists(): cart = cart.first() cart.number += 1 cart.save() else: cart = Cart() cart.user = user cart.goods = good cart.number = 1 cart.save() response_data['status'] = 1 response_data['num'] = cart.number return JsonResponse(response_data) response_data['status'] = -1 return JsonResponse(response_data)
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 addgoods(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') goodsnum = int(request.GET.get('goodsNum')) goods = GOODs.objects.get(pk=goodsid) # 判断需要添加的商品是否已经存在 carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.number += goodsnum cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.number = goodsnum 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_cart(request): token = request.session.get('token') userid = cache.get(token) goodsid = request.GET.get('goodsid') number = int(request.GET.get('number')) print(number) if userid: user = User.objects.get(pk=userid) goods = Goods.objects.get(pk=goodsid) cart = Cart.objects.filter(user=user).filter(goods=goods) if cart.exists(): cart = cart.first() cart.number = cart.number + number cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.number = number cart.save() return JsonResponse({'msg': '{}-添加购物车成功,数量为-{}'.format(cart.goods.name, cart.number), 'status': 1}) else: return JsonResponse({'msg': '请先登录,后操作', 'status': 0})
def addcart(request): #获取token 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 addcart(request): goodsid = request.GET.get('goodsid') token = request.session.get('token') userid = cache.get(token) if userid: user = User.objects.get(pk=userid) goods = Goods.objects.get(id=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 = {'msg': '成功添加商品', 'status': 1, 'number': cart.number} else: response_data = {'msg': '未登录', 'status': -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) number = request.GET.get('number') print(number) # 商品不存在: 添加新记录 # 商品存在: 修改number carts = Cart.objects.filter(user=user).filter(goods=goods) cart = Cart() cart.user = user cart.goods = goods cart.number = number cart.save() response_data = '添加 {} 购物车成功: {}'.format(cart.goods, cart.number) return JsonResponse(response_data)
def addcart(request): token = request.session.get('token') userid = cache.get(token) user = None if userid: user = User.objects.get(pk=userid) goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) cart = Cart() carts = Cart.objects.filter(goods=goods).filter(user=user) if carts.exists(): cart = carts.first() cart.number = int(cart.number) + int(request.GET.get('num')) cart.total = float(cart.goods.price[1:]) * int(cart.number) cart.save() else: cart.user = user cart.goods = goods cart.number = request.GET.get('num') cart.total = float(goods.price[1:]) * int(cart.number) cart.save() # 统计 carts = Cart.objects.filter(user=user) sum = 0 num = 0 for cart in carts: sum += float(cart.goods.price[1:]) * int(cart.number) num += cart.number data = {'status': 1, 'msg': '加入购物车成功!', 'num': num, "sum": sum} else: data = {'status': 0, 'msg': '未登录!'} return JsonResponse(data)
def addtocart(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('goodid') color = request.GET.get('color') size = request.GET.get('size') number = request.GET.get('number') goods = Goods.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): carts1 = carts.filter(color=color).filter(size=size) if carts1.exists(): cart = carts1.first() cart.number = int(number) + cart.number cart.save() else: cart = Cart() cart.goods = goods cart.user = user cart.number = number cart.size = size cart.color = color cart.save() else: cart = Cart() cart.goods = goods cart.user = user cart.number = number cart.size = size cart.color = color cart.save() response_data['status'] = 1 response_data['msg'] = '添加 购物车成功' return JsonResponse(response_data) else: response_data['status'] = -1 response_data['msg'] = '请登录后操作' return JsonResponse(response_data)
def addcart(request): # 思路 # 将ajax请求的goodsid获取 # 即当前用户点击的那个商品 # goodsid # 找到对应的商品 goodsid = request.GET.get('goodsid') print(goodsid) # 用户--- token = request.session.get('token') responseData = {'msg': '', 'status': ''} if token: # 登录 user = User.objects.get(token=token) print(user.name) goods = Goods.objects.get(pk=goodsid) print(goods.productname) carts = Cart.objects.filter(goods=goods).filter(user=user) if carts.exists(): # 存在 # 取第一个 cart = carts.first() cart.number = cart.number + 1 # 如果库存数量比我添加的数据还要小 # 那么添加的数据只能为库存数量 if goods.storenums < cart.number: cart.number = goods.storenums cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: # 不存在 cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录 # ajax请求操作,是重定向不了的! # return redirect('axf:login') responseData['msg'] = '请登录后操作' responseData['status'] = '-1' return JsonResponse(responseData)
def addcart(request): token = request.session.get('token') goodid = request.GET.get('goodid') if token: userid = cache.get(token) user = User.objects.get(pk=userid) goods = Goods.objects.get(pk=goodid) carts = Cart.objects.filter(user=user).filter(goods=goods) # print(user,goods,carts) # print(type(user)) if carts.exists(): # print(111111111) cart = carts.first() # print(222222222) cart.number = cart.number + 1 # print(333333333) cart.save() else: # print(4444444444444) cart = Cart() # print(5555555555) cart.user = user # print(6666666666) cart.goods = goods # print(7777777777777777) cart.number = 1 # print(888888888888) cart.save() return JsonResponse({ 'msg': '{}-加入购物车-数量为-{}'.format(cart.goods.productlongname, cart.number), 'status': 1, 'number': cart.number }) else: return JsonResponse({'msg': '请先登录,再进行操作', 'status': 0})
def addcart(request): # 获取token >> user token = request.session.get('token') # 获取商品id goodsid = request.GET.get('goodsid') print(goodsid) data = {} if token: # 登录 # 获取用户 user = User.objects.get(token=token) # 获取商品 goods = Goods.objects.get(pk=goodsid) # 1、 第一次添加的商品是不存在的,要往数据库中添加一条新记录 # 2、 商品已存在,即修改商品数量 # 判断需要添加的商品是否存在 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() return JsonResponse({ 'msg': '{},添加购物车成功'.format(goods.productlongname), 'number': cart.number, 'status': 1 }) else: # 没登录 # ajax操作中,不能重定向 # ajax异步请求操作,数据的传输 # 即ajax请求,如果想页面跳转(服务器重定向不了),客户端处理 # return redirect('axf:login') data['msg'] = '请登录后操作!' data['status'] = -1 return JsonResponse(data)
def addcart(request): # 获取token 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) # 商品不存在: 添加新记录 # 商品存在: 修改number 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) # 未登录 # 因为是ajax操作,所以重定向是不可以的! # return redirect('axf:login') response_data['status'] = -1 response_data['msg'] = '请登录后操作' return JsonResponse(response_data)
def addtocart(request): # goodsid goodsid = request.GET.get('goodsid') token = request.session.get('token') responseData = { 'msg':'', 'status':'' } 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 if 0 < cart.number: cart.number = 1 cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: # 不在 cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录 # ajax请求操作,是重定向不了的! # return redirect('axf:login') responseData['msg'] = '请登录后操作' responseData['status'] = '-1' return JsonResponse(responseData)
def addcart(request): # 有token,就知道是谁 token = request.session.get('token') if token: # 加操作(有登录) user = User.objects.get(token=token) goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) # 第一次操作: 添加一条新记录 # 后续操作: 只需要修改number # 判断该商品是否存在 carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): # 存在,修改numbner cart = carts.first() cart.number = cart.number + 1 cart.save() else: # 添加一条新的记录 cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() return JsonResponse({ 'msg': '{}-添加购物车成功!'.format(goods.productlongname), 'status': 1, 'number': cart.number }) else: # 跳转到登录(未登录) # 在ajax是不能使用重定向 # ajax更多就是用于数据的传输(数据交互) # 问题: 没有登录,就需要跳转到登录页面; # 但在服务端重定向能不能用? 客户端 # return redirect('axf:login') return JsonResponse({'msg': '请登录后操作!', 'status': 0})
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: # 未登录 [跳转到登录页面] # 由于addcart这个是 用于 ajax操作, 所以这里是不能进行重定向!! # return redirect('axf:login') responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): # 获取token token = request.session.get('token') # 响应数据 response_data = {} # 缓存 if token: userid = cache.get(token) # print(userid) #获取用户id if userid: # 已经登录 user = User.objects.get(pk=userid) goodsid = request.GET.get('goodsid') goods = Goods.objects.get(pk=goodsid) # print(user,goodsid) #点击商品后查看是否能获取对应id # 商品不存在: 添加新记录 商品存在: 修改number 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) # return HttpResponse('添加商品到购物车') response_data['status'] = -1 #未登录状态 response_data['msg'] = '请登录后操作' return JsonResponse(response_data)
def addtocart(request): goodid = request.GET.get('goodid') token = request.session.get('token') responseDate = {} if token: #已登录 user = User.objects.get(token=token) goods = Goods.objects.get(pk=goodid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): #如果存在cart cart = carts.first() cart.goodnumber += 1 # print(cart.goodnumber) if cart.goodnumber > int(goods.storenums): cart.goodnumber = goods.storenums cart.save() responseDate['msg'] = '添加到购物车成功' responseDate['status'] = '1' responseDate['number'] = cart.goodnumber return JsonResponse(responseDate) else: #如果不存在cart,创建 cart = Cart() cart.user = user cart.goods = goods cart.goodnumber = 1 cart.save() responseDate['msg'] = '添加到购物车成功' responseDate['status'] = '1' responseDate['number'] = cart.goodnumber return JsonResponse(responseDate) else: responseDate['msg'] = '未登录' responseDate['status'] = '-1' return JsonResponse(responseDate)
def addtocart(request): # goodsid goodsid = request.GET.get('goodsid') responseData = {'msg': '', 'status': '', 'number': 0} token = request.session.get('token') if token: # 登陆 user = User.objects.get(userToken=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 if goods.storenums < cart.number: cart.number = goods.storenums cart.save() responseData['msg'] = '添加成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseData['msg'] = '添加成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: # 未登录 # ajax不能重定向 # return render(request,'mine/login.html') responseData['msg'] = '请登陆后操作' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request): token = request.session.get('username') print('添加购物车') print('token',token) user = User.objects.filter(username=token)[0] goodid = request.GET.get('goodid') good = Mobile.objects.get(pk=goodid) if token: # pass cart = Cart.objects.filter(user=user,goods=good) if cart: cart = cart[0] cart.number +=1 else: cart = Cart() cart.user = user cart.goods = good cart.number = 1 cart.save() return JsonResponse({'msg':'ok'}) else: print(token) return redirect(reverse('app:registered'))
def addcart(request): goodsid = request.GET.get('goodsid') token = request.session.get('token') responseData = {} 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 if cart.number > goods.storenums: cart.number = goods.storenums cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: cart = Cart() cart.user = user cart.goods = goods cart.number = 1 cart.save() responseData['msg'] = '添加购物车成功' responseData['status'] = 1 responseData['number'] = cart.number return JsonResponse(responseData) else: responseData['msg'] = '未登录' responseData['status'] = -1 return JsonResponse(responseData)
def addcart(request, goods_id): num = request.COOKIES.get('num') token = request.session.get('token') userid = cache.get(token) if userid: user = User.objects.get(pk=userid) goods = Goods.objects.get(pk=goods_id) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.number += int(num) cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.number = num cart.save() return redirect('app:goodcart') else: return render(request, 'no_login.html')
def AddToCart(request): goodsid = request.GET.get('goodsid') num = request.GET.get('num') token = request.COOKIES.get('token') responseData = {'msg': '添加购物车成功', 'status': 1} if token: user = User.objects.get(token=token) goods = TimeGoods.objects.get(pk=goodsid) carts = Cart.objects.filter(user=user).filter(goods=goods) if carts.exists(): cart = carts.first() cart.number = int(num) + cart.number cart.save() else: cart = Cart() cart.user = user cart.goods = goods cart.number = num cart.save() return JsonResponse(responseData) else: responseData['msg'] = '未登录,请登录后操作' responseData['status'] = -1 return JsonResponse(responseData)