我在 models.py
中创建了一个产品类,其中有2个类别。我在html页面中成功显示了类别连接器,但没有显示软件,我留下了评论以正确查看html中的数据。如何像使用连接器一样正确显示html中的软件类别?预先感谢您提供任何提示!
class Products(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=250)
short_description = models.CharField(max_length=100)
longDesc = models.TextField()
category = models.CharField(max_length=50)
version = models.DecimalField(max_digits=2, decimal_places=1)
picture = FilerImageField(null=True, blank=True, related_name="products_image")
def __str__(self):
return self.title
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
views.py
包含以下代码:
def products(request):
objconnectors = Products.objects.all().filter(category__iexact='connectors')
contextconn = {'connectors': objconnectors}
# objsoftware = Products.objects.filter(category__iexact='software')
# contextsoft = {'software': objsoftware}
return render(request, 'website/products.html', contextconn, contextsoft)
html
文件包含一个循环,用于显示“产品”模型中类别“连接器”的所有数据。
{% for products in connectors %}
<div class="products animated delay1" data-effect="fadeInUp">
<div class="connectorWrap">
<div class="productsTitle">
<img src="{{ products.picture.url }}">
</div>
<div class="textBox">
<h3>{{ products.title }}</h3>
<p class="connDesc">{{ products.short_description }}</p>
<p class="versionNumber">{{ products.version }}</p>
</div>
</div>
</div>
{% endfor %}
答案 0 :(得分:1)
您可以简单地使用context
字典。
views.py
def products(request):
objconnectors = Products.objects.all().filter(category__iexact='connectors')
objsoftware = Products.objects.filter(category__iexact='software')
context = {
'connectors': objconnectors,
'softwares': objsoftware
}
return render(request, 'website/products.html', context)
HTML文件
{% for products in connectors %}
... do here...
{% endfor %}
与其他类别类似
{% for products in softwares %}
... do here...
{% endfor %}