我正在尝试基于统一模型复制HTML卡:
Models.py
class Uniform(models.Model):
category = models.CharField(choices = CATEGORY_CHOICES, max_length=11)
description = models.CharField(max_length = 50)
price = models.FloatField(max_length = 6)
size = models.CharField(choices=CLOTHES_SIZE, max_length=4)
image = models.ImageField(upload_to='uniforms/')
class Meta:
ordering = ['category']
def __str__(self):
return '{}: {} - {} - ${}'.format(self.category, self.description, self.size, self.price)
Views.py
def item_list(request):
uniform = Uniform.objects.all()
description = Uniform.objects.order_by().values('description').distinct()
category = Uniform.objects.order_by().values('category').distinct()
context = {
'uniform':uniform,
'description': description,
'category': category
}
return render(request, 'apparelapp/item_list.html', context)
html
<div class="row wow fadeIn">
{% for item in description %}
<div class="col-lg-3 col-md-6 mb-4">
<div class="card">
<div class="view overlay">
<img src="" alt="">
<a>
<div class="mask rgba-white-slight"></div>
</a>
</div>
<div class="card-body text-center">
<label>
<h5>{{ item.description }}</h5>
</label>
<h5>
{% if uniform.description %}
<strong>
<label for="">{{ uniform.category }}</label>
</strong>
</h5>
{% endif %}
<!--<h4 class="font-weight-bold blue-text">
<strong>${{item.price}}</strong>
<div class="dropdown">
{% for size in item.size %}
<ul>
{{size}}
</ul>
{% endfor %}
</div>
</h4> -->
</div>
</div>
</div>
{% endfor %}
我得到的是项目描述,而不是项目类别:我想说的是,如果显示了制服的描述,请显示制服的类别-但为空白
[我尝试过格式化以仅调用统一类别,但随后我的卡数是原来的4倍(例如:围裙小到围裙xlarge的卡片)
uniform
的类别。更容易的是遍历您的Uniform
项目。您可以将其所有项目传递到上下文:
Views.py
def item_list(request):
uniform = Uniform.objects.all()
context = {'uniform': uniform}
return render(request, 'apparelapp/item_list.html', context)
HTML
<div class="row wow fadeIn">
{% for item in uniform %}
<div class="col-lg-3 col-md-6 mb-4">
<div class="card">
<div class="view overlay">
<img src="" alt="">
<a>
<div class="mask rgba-white-slight"></div>
</a>
</div>
<div class="card-body text-center">
<label>
<h5>{{ item.description }}</h5>
</label>
<h5>
{% if item.description %}
<strong>
<label for="">{{ item.category }}</label>
</strong>
</h5>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
而且,我对您的文件进行了一些格式化。HTML