Beispiel #1
0
def addtocart(request, id):
    url = request.META.get('HTTP_REFERER')
    checkproduce = ShopCart.objects.filter(produce_id=id)
    if request.method == 'POST':
        form = ShopCartForm(request.POST)
        if form.is_valid():
            current_user = request.user
            data = ShopCart()
            data.user_id = current_user.id
            data.produce_id = id
            data.quantity = form.cleaned_data['quantity']
            data.save()

        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()
        messages.success(request,
                         "Ürün başarı ile sepete eklendi.Teşekkür ederiz")
        return HttpResponseRedirect(url)

    else:
        current_user = request.user
        data = ShopCart()
        data.user_id = current_user.id
        data.produce_id = id
        data.quantity = 1
        data.save()
        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()
        messages.success(request,
                         "Ürün başarı ile sepete eklendi.Teşekkür ederiz")
        return HttpResponseRedirect(url)

    messages.warning(request,
                     "Ürün sepete eklemede hata oluştu.Lütfen kontrol ediniz")
    return HttpResponseRedirect(url)
Beispiel #2
0
def addtocart(request, id):
    url = request.META.get('HTTP_REFERER')
    current_user = request.user

    scart = ShopCart.objects.filter(produce_id=id)
    if scart:
        control = 1
    else:
        control = 0

    if request.method == 'POST':  #ürün formdan geldiyse
        form = ShopCartForm(request.POST)
        if form.is_valid():
            if control == 1:
                data = ShopCart.objects.get(produce_id=id)
                data.quantity += form.cleaned_data['quantity']
                data.save()
            else:
                data = ShopCart()
                data.user_id = current_user.id
                data.produce_id = id
                data.quantity = form.cleaned_data['quantity']
                data.save()
        request.session['cart_items'] = ShopCart.objects.filter(
            user_id=current_user.id).count()
        messages.success(request, "Ürün sepete eklendi")
        return HttpResponseRedirect(url)
    else:  #ürün direk eklendiyse
        if control == 1:
            data = ShopCart.objects.get(produce_id=id)
            data.quantity += 1
            data.save()
        else:
            data = ShopCart()
            data.user_id = current_user.id
            data.produce_id = id
            data.quantity = 1
            data.save()
            messages.success(request, "Ürün sepete eklendi")
            return HttpResponseRedirect(url)
    request.session['cart_items'] = ShopCart.objects.filter(
        user_id=current_user.id).count()
    messages.warning(request, "Ürün eklemede hata oluştu")
    return HttpResponseRedirect(url)