如果要选择某种产品,我正在尝试规定一个条件。我正在尝试在模板中规定一个条件if
。但是我做错了。请帮助我。
views.py
def product_list(request, category=None, subcategory=None, kind=None):
if category:
categories = Category.objects.all()
category = Category.objects.get(slug=category)
subcategories = Subcategory.objects.filter(category=category)
products = Product.objects.filter(category=category, available=True)
products_quantity = len(Product.objects.filter(category=category, available=True))
kinds = None
if subcategory:
subcategory = Subcategory.objects.get(slug=subcategory)
kinds = Kind.objects.filter(kind=subcategory)
products = Product.objects.filter(category=category, subcategory=subcategory, available=True)
if kind:
kind = Kind.objects.filter(slug=kind)
products = Product.objects.filter(category=category, subcategory=subcategory, kind__in=kind, available=True)
if products:
paginator = Paginator(products, 8)
page = request.GET.get('page')
products = paginator.get_page(page)
context = {
'categories':categories,
'category':category,
'subcategories':subcategories,
'subcategory':subcategory,
'products':products,
'products_quantity':products_quantity,
'kinds':kinds
}
return render(request, 'shop/product/product_list.html', context)
product_list.html
# Not works
{% if kind %}
Hello
{% endif %}
但是,如果我规定了这样的条件。这项工作很好
{% if category %}
Hello
{% endif %}
或者尝试一下。但是不一样
{% if category and subcategory and kind %}
Hello
{% endif %}
我该如何设置条件?谢谢!
答案 0 :(得分:1)
替换您的if语句
{% if kind %}
Hello
{% endif %}
使用
{% if kinds %}
Hello
{% endif %}
在上下文词典中,您以种类的名称声明了键值,但在if语句中,您使用的是种类