我有一张展示所有产品的桌子。我想显示每个产品名称的产品属性。是否可以在如下所示的一个表中执行此操作
多谢各位
型号.py
class Product(models.Model):
name = models.CharField(max_length=60, unique=True)
attribute = models.ManyToManyField(ProductAttribute)
class ProductAttribute(models.Model):
property = models.CharField(max_length=20) # eg. "resolution"
value = models.CharField(max_length=20) # eg. "1080p"
file.html
{% for product in products %}
<tr>
<td style="font-weight:bold">{{ product.name }}</td>
<td>{{ product.productattribute }}</td>
</tr>
{% endfor %}
视图.py
@login_required(login_url="/login/")
def productlist_details(request, shop_id, productlist_id):
shop = Shop.objects.get(pk=shop_id)
products = Product.objects.all()
productattributes = ProductAttribute.objects.all()
context = {
'shop': shop,
'products': products,
'productattributes': productattributes,
}
return render(request, 'productlist_details.html', context)
试试这个
使用
ifchanged
标记(https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#ifchanged),可以执行以下操作:相关问题 更多 >
编程相关推荐