【问题标题】:How can I show in cart items in my index view?如何在我的索引视图中显示购物车项目?
【发布时间】:2021-06-10 15:54:19
【问题描述】:

我正在开发一个电子商务应用程序,并且我有一个显示我所有产品的模板。

我想显示购物车中有多少商品。但我似乎无法弄清楚如何从我的 for 循环中获取这些信息。

我想检查我的 for 循环中的当前产品是否与该用户的 OrderItem 匹配。

我可以在详细视图中执行此操作,但不能在我的目录/索引中执行此操作

代码:

模板

{% for product in products %}

{{product.title}} - {{product.quantity_in_cart}}

{% endfor %}

(截至目前,product.quantity_in_cart 什么都不做。我希望它显示购物车中有多少此产品)

查看

def product_index(request):
title = "My Products"
products = Product.objects.all()
order = get_cart(request)
cart = Order.objects.get(id=order.id, complete=False)
items = cart.orderitem_set.all()
context = {
'title' : title,
'products' : products,
'cart' : cart,
'items': items
}
return render(request, "store/product_index.html", context)

型号

class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
complete = models.BooleanField(default=False, null=True, blank=False)

class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)

class Product(models.Model):
name = models.CharField(max_length=64)
slug = models.SlugField(unique=True, null=True)
description = RichTextField(blank=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
quantity = models.IntegerField()

【问题讨论】:

    标签: python django jinja2


    【解决方案1】:
    def product_index(request):
    
        title = "My Products"
        products = Product.objects.all()
        order = get_cart(request)
        cart = Order.objects.get(id=order.id, complete=False)
        items = cart.orderitem_set.all()
    
        # Changes
        for product in products:
            qty = 0
            if items.filter(product=product).exists():
                qty = items.get(product=product).quantity
            setattr(product, 'quantity_in_cart', qty)
    
        context = {
        'title' : title,
        'products' : products,
        'cart' : cart,
        'items': items
        }
        return render(request, "store/product_index.html", context)
    

    【讨论】:

    • 谢谢!如果您将 items.get(product=product).qty 更改为 items.get(product=product).quantity,这将非常有效
    • 是的,对不起,我写了我自己模型的“数量”字段.. 仍然像 @mohamed-elkalioby 所说的那样,这不是一个好的方法,但正是你想要的。
    【解决方案2】:

    添加一个名为“cart”的会话键,它可以是一个列表,列表中的每个项目都可以是一个包含“名称”、“数量”和价格的字典,因为用户正在添加到您添加的购物车session 变量,在模板中,您可以使用这样的 for 呈现购物车

    {% for product in request.session.carr%}
     {%endfor%}
    

    【讨论】:

    • 我实际上已经有了,但我需要做的是显示所有产品,然后检查每个产品是否在我当前用户的购物车中,并显示 OrderItem 的数量而不是产品
    • 但这会很慢,你为什么要这么做?
    • 商品也可以从索引添加到购物车,我希望用户能够看到他们已经拥有多少商品或商品是否在他们的购物车中
    • 就是session的思路,ppl可以从任何地方添加item,都去session
    • 这很好,会话已经包含购物车数据(由数据库备份),问题是查询目录中的每个项目是否在当前会话/用户购物车中。接受的答案显示了我正在尝试做的解决方案
    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2021-02-24
    • 2019-09-18
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多