Exemplo n.º 1
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:   #未登录[跳转到登陆页面]
        responseData['msg'] = '未登录,请登录后操作'
        responseData['status'] = -1
        return JsonResponse(responseData)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
Arquivo: views.py Projeto: buddysh/AXF
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)
Exemplo n.º 4
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)
        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)
Exemplo n.º 5
0
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)