예제 #1
0
def carJump(request, goods_id):
    goods = Goods.objects.get(id=int(goods_id))
    id = request.COOKIES.get("user_id")  # 获取用户身份
    if request.method == "POST" and request.POST:
        count = request.POST.get("count")
        img = request.POST.get("good_img")
        # try:
        buyCar = BuyCar.objects.filter(
            user=int(id), goods_id=int(goods_id)).first()  #查询是否存在在购物车当中
        # except Exception as  e:
        #     print(e)

        if not buyCar:  #不存在
            buyCar = BuyCar()  #实例化模型
            buyCar.goods_num = int(count)  #添加数量
            buyCar.goods_id = goods.id
            buyCar.goods_name = goods.goods_name
            buyCar.goods_price = goods.goods_now_price
            buyCar.user = Buyer.objects.get(id=request.COOKIES.get("user_id"))
            buyCar.save()
        else:  #存在
            buyCar.goods_num += int(count)  #数量相加
            buyCar.save()
        all_price = float(buyCar.goods_price) * int(count)
        return render(request, "buyer/buyCar_jump.html", locals())
    else:
        return HttpResponse("404 not fond")
예제 #2
0
def carJump(request, goods_id):
    goods = Goods.objects.get(id=int(goods_id))
    id = request.COOKIES.get("user_id")
    if request.method == "POST" and request.POST:
        count = request.POST.get("count")
        img = request.POST.get("good_img")
        buyCar = BuyCar.objects.filter(user=int(id),
                                       goods_id=int(goods_id)).first()
        if not buyCar:
            buyCar = BuyCar()
            buyCar.goods_id = goods.id
            buyCar.goods_name = goods.goods_name
            buyCar.goods_price = goods.goods_now_price
            buyCar.goods_num = int(count)
            buyCar.user = Buyer.objects.get(id=request.COOKIES.get("user_id"))
            buyCar.save()
        else:
            buyCar.goods_num += int(count)
            buyCar.save
        all_price = float(buyCar.goods_price) * int(count)
        return render(request, 'buyer/carJump.html', locals())
    else:
        return HttpResponseRedirect("404 not found")
예제 #3
0
def add_car(request):
    result = {"statue": "error", "data": ""}
    if request.method == "POST":
        user = request.COOKIES.get("email")
        goods_id = request.POST.get("goods_id")
        number = request.POST.get("number", 1)

        try:
            goods = Goods.objects.get(id=goods_id)
        except Exception as e:
            result["data"] = str(e)
        else:
            car = BuyCar()
            car.car_user = user
            car.goods_name = goods.name
            car.goods_picture = goods.picture
            car.goods_price = goods.price
            car.goods_number = number
            car.goods_total = number * goods.price
            car.goods_store = goods.goods_store_id
            car.save()
            result["statue"] = "success"
            result["data"] = "加入购物车成功"
        return JsonResponse(result)