我有一个名为product的模型,并且我想根据产品的类型传递表格。因此,例如,如果产品类型为“服装”,则渲染表格以选择尺寸(S,M,L等)
我尝试创建一个将产品传递到模板的视图,然后在名为is_clothing的视图中创建一个函数,该函数表示产品类型为服装。
# models.py
PRODUCT_TYPE = [
('C', 'Clothing'),
('A', 'Accessory'),
('F', 'Furniture'),
]
class Product(models.Model):
...
type = models.Charfield(blank=True, choices=PRODUCT_TYPE)
# views.py
def product_view(request, slug):
productvariant = get_object_or_404(ProductVariants, slug=slug)
form = ProductSizeForm
# Function that defines whether product type is clothing
def is_clothing():
product.type == "Clothing"
context = {
'product' : productvariant,
'form' : form
}
return render(request, "products/product-page.html", context)
<!--TEMPLATE-->
{% if product.is_clothing %}
{{ form }}
{% endif %}
正如我说的那样,如果产品类型是服装,我希望呈现尺码表,但是我没有得到任何结果。有什么想法吗?