Django 为什么这种情况不起作用如果在模板中
我试着开一个条件,如果选择的产品种类。我试图在模板中规定一个条件Django 为什么这种情况不起作用如果在模板中,django,django-templates,Django,Django Templates,我试着开一个条件,如果选择的产品种类。我试图在模板中规定一个条件if。但我做错了什么。请帮帮我 视图.py def product_list(request, category=None, subcategory=None, kind=None): if category: categories = Category.objects.all() category = Category.objects.get(slug=category) s
if
。但我做错了什么。请帮帮我
视图.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)
产品列表.html
# Not works
{% if kind %}
Hello
{% endif %}
但如果我开了这样一个条件。这工作做得很好
{% if category %}
Hello
{% endif %}
或者试试这个。但效果不一样
{% if category and subcategory and kind %}
Hello
{% endif %}
我如何为实物设定条件?谢谢 替换您的if语句
{% if kind %}
Hello
{% endif %}
与
在您的上下文词典中,您声明的键值名称为种类,但在if语句中,您使用的是种类谢谢!有时我也会面对这种疯狂的错误
{% if kinds %}
Hello
{% endif %}