I am trying to run a conditioanl test in a Django Template based on a count of child objects.
I have passed a varible to the Template called “productcodes” and I can successfully iterate through the each item within it.
Models
class Productcode(models.Model):
product_code = models.CharField(max_length=50)
def __str__(self):
return self.product_code
class Test(models.Model):
worksorder = models.ForeignKey(Worksorder, blank=True, null=True, on_delete=models.CASCADE,)
productcode = models.ForeignKey(Productcode, blank=True, null=True,on_delete=models.CASCADE)
tester = models.ForeignKey(Tester, blank=True, null=True,on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return self.date
{% if productcodes %}
{% for code in productcodes %}
<tr>
<th scope="row"><a href="show_productcode/{{ code.id }}"> {{ code }} </a></th>
{% if code.test_set.all.count < 1 %}
<td> 0 </td>
{% elif %}
<td>{{ code.test_set.all.count }}</td>
{% endif %}
<td>Future Dev WO</td>
</tr>
{% endfor %}
{% endif %}
The issue I am having is this if statement is failing with the error:
Unexpected end of expression in if tag.
{% if code.test_set.all.count < 1 %}
I’ve tested the expression on the shell ok, am I missing something obvious?
Best Regards
Colin.
I am expecting a count of child objects to br printed in the table.
user25252853 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1