Django中的一个模型有两个类别。如何在html中显示同一模型中的另一个类别?
我在models.py中创建了一个类产品,其中我有两个类别。我成功地在我的html页面中显示了类别连接器,但没有显示我留下注释的软件,以便在html中正确查看数据。如何像使用连接器一样,以html格式正确显示软件类别?提前感谢您提供的任何提示Django中的一个模型有两个类别。如何在html中显示同一模型中的另一个类别?,django,django-models,django-views,Django,Django Models,Django Views,我在models.py中创建了一个类产品,其中我有两个类别。我成功地在我的html页面中显示了类别连接器,但没有显示我留下注释的软件,以便在html中正确查看数据。如何像使用连接器一样,以html格式正确显示软件类别?提前感谢您提供的任何提示 class Products(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(max_length=250) short
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'
视图.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 %}
{%用于连接器中的产品%}
{{products.title}
{{products.short_description}
{{products.version}
{%endfor%}
您只需使用上下文
dict即可
视图.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 %}
非常感谢你的小费!我很高兴它起了作用。您可以向上投票并将答案标记为选中。谢谢