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 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': '添加成功'})