0

I am trying to figure out the best way to loop a given number of times within a Django template from an integer field

For example, if I have a model:

models.py

class Rating(models.Model):
   checked = models.IntegerField(default='0')
   unchecked = models.IntegerField(default='0')

And I have a record where checked = 5, in the template how can I do the following:

{% for i in checked %}
<div>some repeatable content here </div>
{% endfor %}

Since checked is an integer, it is a non-iterable and it cannot be used in the for-loop. Is there a more simple solution?

Hayden
  • 498
  • 2
  • 5
  • 18

1 Answers1

2

pass range(checked) to template and then iterate. Or you can create property:

@property
def checked_range(self):
    return range(self.checked)

and use it in template like so:

{% for i in rating.checked_range %}
    <div>some repeatable content here </div>
{% endfor %}