如果产品在心愿单或没有,请在模板中过滤.Django电子商务网站



我有一个关于django模板的问题。我想知道我们是否可以在django模板中添加一个过滤器。我想知道产品在渲染时是否在愿望列表中。那么,我们如何才能知道它是否在愿望清单中呢?views.py

def store(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
categories = Category.objects.all()
products = Product.objects.all()
if request.user.is_authenticated:
user=request.user
wishedProducts = Wishlist.objects.filter(user=user)
else:
wishedProducts = {}
"""popularnye = Product.objects.filter(popularnye=True)"""
context = {'products':products, 'cartItems':cartItems, 'order':order, 'items':items, 'categories':categories,'wishedProducts':wishedProducts}
return render(request, 'store/store.html', context)

型号.py

class Wishlist(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, verbose_name="Название товара",null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
unique_together = (
('user', 'product'),
)
verbose_name = 'Избранные'
verbose_name_plural = "Избранные"
def __str__(self):
return str(self.product)

模板

{% for product in products %}
<div class="store col-lg-3 col-6">
<div class="single-product">
<div class="single-product">
<div class="header-single-product">
<p style="margin-top:-10px;margin-bottom:-10px" class="code">Код: 51265</p>
{% if product in wishedProducts.product %}
<i class="bi bi-heart-fill addWishlist" style="color: red" data-product="{{product.id}}" data-action="add"></i>
{% else %}
<i class="bi bi-heart addWishlist"  data-product="{{product.id}}" data-action="add"></i>
{% endif %}
<i class="fa fa-balance-scale" style="margin-right:5px;"></i>

我通过用wishlist的产品创建另一个列表来解决这个问题。views.py

if request.user.is_authenticated:
user=request.user
wishedProducts = Wishlist.objects.filter(user=user)
wishedProductsList = []
for i in wishedProducts:
wishedProductsList.append(i.product)
count = Wishlist.objects.filter(user=user).count()

else:
wishedProducts = {}
wishedProductsList={}

模板

{% for product in products %}
<div class="store col-lg-3 col-6">
<div class="single-product">
<div class="single-product">
<div class="header-single-product">
<p style="margin-top:-10px;margin-bottom:-10px" class="code">Код: 51265</p>
{% if product in wishedProductsList %}
<i class="bi bi-heart-fill addWishlist" style="color: red" data-product="{{product.id}}" data-action="add"></i>
{% else %}
<i class="bi bi-heart addWishlist"  data-product="{{product.id}}" data-action="add"></i>
{% endif %}
<i class="fa fa-balance-scale" style="margin-right:5px;"></i>

您可以通过以下方式获得产品:

def store(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
categories = Category.objects.all()
products = Product.objects.all()
if request.user.is_authenticated:
wishedProducts = Product.objects.filter(wishlist__user=request.user)
else:
wishedProducts = Product.objects.none()
context = {'products':products, 'cartItems':cartItems, 'order':order, 'items':items, 'categories':categories,'wishedProducts':wishedProducts}
return render(request, 'store/store.html', context)

这将在单个查询中获取愿望列表中的所有Product,从而避免N+1问题

相关内容

  • 没有找到相关文章

最新更新