Home > Software engineering >  How do I loop a intergerFIeld in django templates?
How do I loop a intergerFIeld in django templates?

Time:07-26

I have to loop an element according to the dynamic data. I have an INTeger Field. Here's what I want to loop:

<li ><i ></i></li>

about 5 times max and 1 min.

My model:

class Clients(models.Model):
image = models.ImageField(upload_to='clients/', null=True)
timestamp = models.DateTimeField(auto_now_add=True)
company = models.ForeignKey(connectedCompanies, on_delete=models.PROTECT, null=True)
rating_Given = models.IntegerField(verbose_name='Rating out of 5: ', null=True)
description = models.CharField(max_length=500)
name = models.CharField(max_length=50)
post = models.CharField(max_length=50, null=True)
def __str__(self):
    return self.name

@property
def get_rating_Given_range(self):
    return range(self.rating_Given)

Thanks

CodePudding user response:

try to use the model property that converts IntegerField to range object:

in model:

@property
def get_rating_Given_range(self):
    return range(self.rating_Given or 0) #change 0 to 1 if you need to loop at least 1.

in template

{% for i in objetc.get_intfield_range %}
    <li ><i ></i></li>
{% endfor %}
  • Related