I'm trying to loop through different Zones and then show the items which are part of this zone
Zone is a model, has a name and a ForeignKey. Planche is a model which has Zone as ForeignKey.
I'm looping through zones to display each zone. In that loop I'm looping all Planches and would like to display only the ones that have Zone as a ForeignKey.
class Zones(models.Model):
name = models.CharField(max_length=30)
genre = models.ForeignKey(ZoneTypes, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Planche(models.Model):
pzone = models.ForeignKey(Zones, on_delete=models.CASCADE)
ref = models.CharField(max_length=5, default="1")
length = models.IntegerField()
width = models.IntegerField()
orientation = models.CharField(max_length=30)
def __str__(self):
return self.ref
Template
<div>
<h1><a href="/">My list of planches</a></h1>
</div>
{% for z in zones %}
<div>
<h2><a href="/zone/{{ z.name }}">Zone name: {{ z.name }}</a></h2>
{% for p in planches %}
{% if p.pzone == z.name }
<h1><a href="planche/{{ planche.ref }}">Ref: {{ p.ref }}</a></h1>
<p>Length: {{ p.length }} - Width: {{ p.width }}</p>
<p>Orientation: {{ p.orientation }}
{% endif %}
{% endfor %}
</div>
{% endfor %}
{% if p.pzone = z.name %} returns False, They both return the same string if I just display them {{ p.pzone }} and {{ z.name }} but I guess they aren't the same data type. I tried converting them to strings in a {% with %} statement but I keep failing