0

I am working on an implementation of an online shop using cartridge & mezzanine. I would like to include a "featured products" section using a category from cartridge's shop (product_category). I have created a test category, populated it with entries and am trying to view it from the homepage but it doesn't return anything. What should i be doing instead?

index.html

{% with category as "featured" %}
{% for product in products.object_list %}

(just using the category.html product display html for now)

views.py

def index(request):
    products = Product.objects.all()
    context = {'products': products}
    return render(request, 'index.html', context)
4

1 回答 1

0

您在 index.html 中的代码:

{% 类别为“精选”%}

意味着将变量category视为字符串文字(您确定这是您想要的吗?),因此您将无法featured在此语句之后使用变量。

用单引号 (') 或双引号 (") 括起来的值被视为字符串文字,而没有引号的值被视为模板变量。(Django 文档)

接下来,根据您的代码,您products的上下文中有变量(这是 QuerySet),因此 value products.object_list 不会给出任何内容,因为 QuerySet 对象没有object_list属性。也许你在这一行想要的是

{% for product in products %}

您可以发布更多代码以澄清情况。

于 2015-11-26T10:59:47.863 回答