从模型导入类别,产品

2024-03-29 08:08:53 发布

您现在位置:Python中文网/ 问答频道 /正文

问题与“从模型进口类别,产品”。。。你知道吗

def ProductList(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=category)
    return render(request, 'shop/product/list.html', {
        'category': category,
        'categories': categories,
        'products': products
    })

Tags: 模型noneobjects产品requestdefallfilter