Python Django模板-具有特定属性的渲染对象
我希望我的模板渲染具有特定属性的对象(具有特定类别的产品)。 我正在尝试下面的代码,但它不工作。 你能建议如何正确地写吗 我正在尝试使用的模板:Python Django模板-具有特定属性的渲染对象,python,django-models,django-templates,Python,Django Models,Django Templates,我希望我的模板渲染具有特定属性的对象(具有特定类别的产品)。 我正在尝试下面的代码,但它不工作。 你能建议如何正确地写吗 我正在尝试使用的模板: {% for instance in object_list %} {% if instance.category == 'Statues' %} {% endif %} {% endfor %} my型号.py class Category(models.Model): categoryname = models.CharField(max
{% for instance in object_list %}
{% if instance.category == 'Statues' %}
{% endif %}
{% endfor %}
my型号.py
class Category(models.Model):
categoryname = models.CharField(max_length=20)
description = models.CharField(max_length=200, blank=True, null=True)
#To make in name, not objXXX
def __str__(self):
return self.categoryname
class Product(models.Model):
name = models.CharField(max_length=20)
image = models.ImageField(upload_to='static/photos', default='http://placehold.it/700x400')
description = models.TextField(max_length=200, blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10)
category = models.ForeignKey(Category, on_delete=models.PROTECT, blank=True, null=True)
#To make in name, not objXXX
def __str__(self):
return self.name
您需要使用对象上的字段
{对象列表%中的实例%}
{%if instance.category.categoryname=='状态'%}
{{instance}}
{%endif%}
{%endfor%}
它无法与对象的字符串表示形式进行比较