-1

I want to display in a html as many tables as there are loops in a tournament object.

I need to iterate by value in field loop_count of Tournament model (the numbers themselves in the list are not needed). I didn't find a way on the Internet, so I came up with it myself. With property, is this the best way for do it?

Tournament model:

class Tournament(models.Model):
    name = models.CharField("Title",max_length=50,unique=True)
    loop_count = models.PositiveSmallIntegerField("Count of loop")

    @property
    def loop_count_list(self):
        return range(self.loop_count)

Html:

{% for loop in tournament.loop_count_list %}
<div class="home-champ"...>
{% endfor %>

And if I need the numbers themselves in the list, I can do the following: (i know about forloop.counter)

Html:

{% for loop in tournament.loop_count_list %}
<b>Index: {{ loop }}</b>
<div class="home-champ"...>
{% endfor %>
Mauzzz0
  • 53
  • 1
  • 6
  • Html is a DetailView of tournament – Mauzzz0 Feb 25 '21 at 23:58
  • Does this answer your question? [Numeric for loop in Django templates](https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates) – Iain Shelvington Feb 25 '21 at 23:58
  • No, bcs this answer has no relation with the model's field – Mauzzz0 Feb 26 '21 at 00:02
  • I'd take a look and see if there is a way to iterate over whatever is represented in that div and use the forloop counter you mentioned (or a html ordered list for that matter) for the index number. It would be interesting to see how you are going to show your details in the div in the way you have it now. – AMG Feb 26 '21 at 04:35

1 Answers1

0

If this works for you, I'd go with it.

You could write a custom tag to accomplish nearly the same thing.

There is also a hacky (but clever) way here: https://stackoverflow.com/a/14389078/4872140

Questions about the 'best' way are tricky and discouraged.

AMG
  • 1,606
  • 1
  • 14
  • 25