简体   繁体   中英

Django Cart Model with products - implementing quantity of items

I've got a Cart, Product and a Entry model. What I'm trying to do is giving users the option to put more then one item in the cart and displaying the quantity in the checkout. I can get the chosen quantity over

quantity_input= request.POST.get('quantity-field')

and create a new Entry object within the cart_update()

Entry.objects.create(cart=cart_obj, product=product_obj, quantity=quantity_form)

which knows what cart and product it belongs to. But then I hit a wall in outputting it over the view since I'm only having a cart_obj as context not knowing how to also render the entry object in addition.

Cart Model:

class Cart(models.Model):
        user        = models.ForeignKey(User, null=True, blank=True)
        products    = models.ManyToManyField(Product, blank=True)
        subtotal    = models.DecimalField(default=0.00, decimal_places=2, max_digits=100)
        total       = models.DecimalField(default=0.00, decimal_places=2, max_digits=100)    
        count       = models.PositiveIntegerField(default=0)

        objects = CartManager()

Entry Model

class Entry(models.Model):
        product = models.ForeignKey(Product, null=True)
        eCart = models.ForeignKey(Cart, null=True)
        quantity = models.PositiveIntegerField()

@receiver(post_save, sender=Entry)
    def update_cart(sender, instance, **kwargs):
        line_cost = instance.quantity * instance.product.price
        instance.cart.count = int(instance.cart.count) + int(instance.quantity)

carts views.py

def cart_update(request):
    product_id = request.POST.get('product_id')
    quantity_input= request.POST.get('quantity-field')

    if product_id is not None:            
        cart_obj, new_obj = Cart.objects.new_or_get(request)
        Entry.objects.create(cart=cart_obj, product=product_obj, quantity=quantity_input)
        cart_obj.products.add(product_obj)            
        added = True
        request.session['cart_items'] = cart_obj.products.count()        
    return redirect("carts:home")

def cart_home(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    return render(request, "carts/home.html",{"cart":cart_obj})

Thanks for help.

If I understand the first part of your question, you want to know how to pass more than one thing in the context part of your return render(request, "carts/home.html",{"cart":cart_obj}) .

One way to do this is to store all the template variables you want in a dict and then pass the dict as the context, like so:

template_vars = {
   'cart': cart_obj,
   'entry': entry_obj,
   'some_var': some_value,
}

return render(request, "carts/home.html", template_vars)

Or you could do it the way you've got it right now, just adding stuff in:

    return render(request, "carts/home.html", {
        'cart': cart_obj,
        'entry': entry_obj,
        'some_var': some_value,  
    })

Let me know if that helps at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM