我有一个单一的产品页面,其中有一个添加到购物车按钮,但如果该产品已经存在于购物车中,我想显示一个添加至购物车图标。我不知道该怎么查。这是我的型号。py
class Order(models.Model):
customer=models.ForeignKey(Customer,on_delete=models.SET_NULL,null=True,blank=True)
date_ordered=models.DateTimeField(auto_now_add=True)
complete=models.BooleanField(default=False,null=True,blank=False)
transaction_id=models.CharField(max_length=100,null=True)
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=False)
date_added=models.DateTimeField(auto_now_add=True)
class Product(models.Model):
category = models.ForeignKey(Category, on_delete = models.CASCADE)
productid=models.CharField(max_length=30)
name=models.CharField(max_length=30)
这是我的观点。py
def singleproduct(request, id):
context = {
'types': Category.objects.all(),
'prods': Product.objects.filter(productid = id),
'in_cart': False,
}
if request.user.is_authenticated:
order = Order.objects.get_or_create(customer=request.user.customer, complete=False)[0]
context['in_cart'] = order.products.filter(product_id=id).exists()
return render(request,"singleproduct.html", context)
def cart(request):
customer=request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete=False)
items=order.orderitem_set.all()
context={
'items':items,
'order':order
}
return render(request,"cart.html",context)
def category(request):
context = {
'types' : Category.objects.all(),
'prods': Product.objects.filter(),
'cartItems':[],
'in_cart': False,
}
if request.user.is_authenticated:
customer=request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete=False)
cartItems=order.get_cart_items,
items=order.orderitem_set.all()
context['in_cart'] =order.orderitem_set.filter(product__productid=id).exists()
return render(request,"category.html",context)
这是我的html代码用于该按钮
{% if user.is_authenticated %}
{% if in_cart %}
<button data-product={{product.id}} data-action="add"
class="btnabc btnabc-warning update-cart">Added</button>
{% else %}
<button data-product={{product.id}} data-action="add"
class="btnabc btnabc-warning update-cart">Add to Cart</button>
{% endif %}
{% else %}
<button class="btnabc btnabc-warning"><a href="/login/">Add to
Cart</a></button>
{% endif %}
但它似乎不起作用,即使产品不在购物车中,它显示添加。请建议我一种方法,这样我就可以在我的html代码上检查商品是否存在于购物车中并相应地显示按钮。任何帮助都会得到通知.
if request.user.is_authenticated:
customer=request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete=False)
items=order.orderitem_set.all()
cartItems=order.get_cart_items
types = Category.objects.all()
prods= Product.objects.filter(productid = id)
# filter the oder by the product_id , and return True if exsits
in_cart = order.orderitem_set.filter(product__productid=id).exists()
else:
types = Category.objects.all()
prods= Product.objects.filter(productid = id)
cartItems=[]
# default False
in_cart = False
# no forget to add in context
return render(request,"singleproduct.html", {'types':types,'prods':prods, 'in_cart': in_cart})
在html:中
{% if in_cart %}
already in cart
{% else %}
Add in cart
{% endif %}
编辑:你可以这样优化你的视图,点完全相同的
def singleproduct(request, id):
context = {
'types': Category.objects.all(),
'prods': Product.objects.filter(productid = id),
'in_cart': False,
}
if request.user.is_authenticated:
order = Order.objects.get_or_create(customer=request.user.customer, complete=False)[0]
context['in_cart'] = order.orderitem_set.filter(product__productid=id).exists()
return render(request,"singleproduct.html", context)