Hello I want to use my counter variable from my for loop inside an interpolation tag.
My code right now:
{% for i in list %}
<tr>
<td>{{ forloop.counter }} Palletbox</td>
<td>
<p id="ipalletBox{{ forloop.counter }}_Leveringp">{{ object.ipalletBox{{ i }}_Levering }} €</p>
</td>
<td>
<input
id="ipalletBox{{ forloop.counter }}_Levering"
name="ipalletBox{{ forloop.counter }}_Levering"
type="text"
class="form-control"
/>
</td>
<td>
<button id="ipalletBox{{ forloop.counter }}_Leveringbtn"
style="border-color: #4e73df"
type="button"
class="btn btn-primary inputCheck"
target="ipalletBox{{ forloop.counter }}_Levering">
Update
</button>
</td>
</tr>
"object.ipalletBox{{ i }}_Levering " ==> I want to be able to do this, but unfortunately it does not work, is there an easy way to use the counter variable inside an interpolation tag?
You are correct, what you’re trying there isn’t going to work.
What is list
in your context? Is it a list of ipalletbox objects?
We’ll need to understand how the data you are working with is structured to offer a solution.
The list consists of an array with numbers from 1-12
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['service'] = Prijzen.objects.all()
context['list'] = [1,2,3,4,5,6,7,8,9,10,11,12]
return context
Where does this service
get used?
What is object.ipalletBox
that you are referencing. What is it’s structure?
I don’t actually use my service inside my template.
My model looks like this:
class Prijzen(models.Model):
leveringsKost = models.FloatField(default=0)
ophalingsKost = models.FloatField(default=0)
huurKost = models.FloatField(default=0)
percentKlant = models.FloatField(default=0)
percentBedrijf = models.FloatField(default=0)
ictPremium = models.FloatField(default=0)
servers = models.FloatField(default=0)
elektroPremium = models.FloatField(default=0)
kleineApparaten = models.FloatField(default=0)
gsmINCL = models.FloatField(default=0)
gsmEXCL = models.FloatField(default=0)
grootWitgoed = models.FloatField(default=0)
profKoelvries = models.FloatField(default=0)
restAfval = models.FloatField(default=0)
inkoopLeveringsKost = models.FloatField(default=0)
inkoopOphalingsKost = models.FloatField(default=0)
inkoopHuurKost = models.FloatField(default=0)
inkoopICTPremium = models.FloatField(default=0)
inkoopServers = models.FloatField(default=0)
inkoopElektroPremium = models.FloatField(default=0)
inkoopKleineApparaten = models.FloatField(default=0)
inkoopGSMINCL = models.FloatField(default=0)
inkoopGSMEXCL = models.FloatField(default=0)
inkoopGrootWitgoed = models.FloatField(default=0)
inkoopProfKoelvries = models.FloatField(default=0)
inkoopRestAfval = models.FloatField(default=0)
############################################
################## Levering ###################
############################################
palletBox1_Levering = models.FloatField(default=0)
palletBox2_Levering = models.FloatField(default=0)
palletBox3_Levering = models.FloatField(default=0)
palletBox4_Levering = models.FloatField(default=0)
palletBox5_Levering = models.FloatField(default=0)
palletBox6_Levering = models.FloatField(default=0)
palletBox7_Levering = models.FloatField(default=0)
palletBox8_Levering = models.FloatField(default=0)
palletBox9_Levering = models.FloatField(default=0)
palletBox10_Levering = models.FloatField(default=0)
palletBox11_Levering = models.FloatField(default=0)
palletBox12_Levering = models.FloatField(default=0)
rolcontainer1_Levering = models.FloatField(default=0)
rolcontainer2_Levering = models.FloatField(default=0)
rolcontainer3_Levering = models.FloatField(default=0)
rolcontainer4_Levering = models.FloatField(default=0)
rolcontainer5_Levering = models.FloatField(default=0)
rolcontainer6_Levering = models.FloatField(default=0)
rolcontainer7_Levering = models.FloatField(default=0)
rolcontainer8_Levering = models.FloatField(default=0)
rolcontainer9_Levering = models.FloatField(default=0)
rolcontainer10_Levering = models.FloatField(default=0)
rolcontainer11_Levering = models.FloatField(default=0)
rolcontainer12_Levering = models.FloatField(default=0)
ipalletBox1_Levering = models.FloatField(default=0)
ipalletBox2_Levering = models.FloatField(default=0)
ipalletBox3_Levering = models.FloatField(default=0)
ipalletBox4_Levering = models.FloatField(default=0)
ipalletBox5_Levering = models.FloatField(default=0)
ipalletBox6_Levering = models.FloatField(default=0)
ipalletBox7_Levering = models.FloatField(default=0)
ipalletBox8_Levering = models.FloatField(default=0)
ipalletBox9_Levering = models.FloatField(default=0)
ipalletBox10_Levering = models.FloatField(default=0)
ipalletBox11_Levering = models.FloatField(default=0)
ipalletBox12_Levering = models.FloatField(default=0)
irolcontainer1_Levering = models.FloatField(default=0)
irolcontainer2_Levering = models.FloatField(default=0)
irolcontainer3_Levering = models.FloatField(default=0)
irolcontainer4_Levering = models.FloatField(default=0)
irolcontainer5_Levering = models.FloatField(default=0)
irolcontainer6_Levering = models.FloatField(default=0)
irolcontainer7_Levering = models.FloatField(default=0)
irolcontainer8_Levering = models.FloatField(default=0)
irolcontainer9_Levering = models.FloatField(default=0)
irolcontainer10_Levering = models.FloatField(default=0)
irolcontainer11_Levering = models.FloatField(default=0)
irolcontainer12_Levering = models.FloatField(default=0)
now you know why I want to access these with the counter variable inside my for loop… haha
Ok, do yourself and your design a favor, and restructure your models.
Depending on whether there’s a relationship between palletBoxX
and rolcontainerX
(and the other similar field), you should have at least 1 and possibly 3 tables with ForeignKey relationships to Prijzen
. You’re creating significantly more work for yourself by trying to get this design to work well.
1 Like
There is no relationship between palletBoxX and rolcontainerX. How would you restructure it in order to loop it inside my template?
Ok, I don’t know your application domain to make any firm recommendations, and I don’t know how else you’re going to use this data throughout your system, so this is all speaking in general terms and may not strictly apply in this case.
You want to work on thinking about these as “sets of data”, and not “arrays with numerical indexes”. Look at iterating over result sets.
As a general rule, I’d be starting to look in a direction like this:
class Prijzen(models.Model):
leveringsKost = models.FloatField(default=0)
...
class PalletBox(models.Model):
prijzen = models.ForeignKey(Prijzen, ...)
levering = models.FloatField(default=0)
class RolContainer(models.Model):
prijzen = models.ForeignKey(Prijzen, ...)
levering = models.FloatField(default=0)
class IPalletBox(models.Model):
prijzen = models.ForeignKey(Prijzen, ...)
levering = models.FloatField(default=0)
class IRolContainer(models.Model):
prijzen = models.ForeignKey(Prijzen, ...)
levering = models.FloatField(default=0)
And rework the rest of the system from that as the starting point.