Python 条件对象类模板标记Django
我试图复制基于统一模型的HTML卡: 型号.pyPython 条件对象类模板标记Django,python,django,Python,Django,我试图复制基于统一模型的HTML卡: 型号.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.CharF
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)
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)
def item_list(request):
uniform = Uniform.objects.all()
context = {'uniform': uniform}
return render(request, 'apparelapp/item_list.html', context)
视图.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)
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)
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 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 %}
{description%%中的项的%1}
我尝试格式化仅调用uniform类别,但随后我调用了4倍数量的卡片(例如:一张用于小围裙到大围裙的卡片)这是因为您使用了错误的变量来访问uniform的类别
更容易的方法是迭代统一的项。您可以将其所有项目传递给上下文:
视图.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)
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)
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>
{uniform%%中的项目为%1}
{{item.description}}
{%if item.description%}
{{item.category}}
{%endif%}
{%endfor%}
另外,我对您的HTML文件进行了一些格式化。这是因为您使用了错误的变量来访问uniform
的类别
更容易的方法是迭代统一的项。您可以将其所有项目传递给上下文:
视图.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)
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)
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>
{uniform%%中的项目为%1}
{{item.description}}
{%if item.description%}
{{item.category}}
{%endif%}
{%endfor%}
另外,我对您的HTML文件进行了一些格式化