1

It's a question by curiosity :

So i have 4 types of choice for field of a model.

class Thing(models.Model):
    Cat_One = (("b", "Big"), ("s", "Small"),("a","very small"),("x","xtra small"))
    dateCreation = models.DateTimeField(null=True, blank=True)
    url = models.CharField(null=True, blank=True, max_length=800)
    name = models.CharField(blank=True, null=True, max_length=200)
    catOne = models.CharField(max_length=1, choices=Cat_One, blank=True, null=True)

so i can pass the choice to the django template :

{% for choice in  cat_One %} 

... And iterate.

But i would like to know , how to iterate from 1 to 4 without passing something to the django template ?

Is there a way to do :

{% for number in [1,2,3,4] %}

or something with a forloop counter ?

regards

Bussiere
  • 500
  • 13
  • 60
  • 119
  • 1
    you mean slicing `cat_One|slice:"0:4"` – Avinash Raj Nov 22 '18 at 09:20
  • @AvinashRaj you gave me the answer it's possible to make a {% for num in slice:"0:4" %} in django. I wanted to know how to iterate numbers without giving anything to the django template. – Bussiere Nov 22 '18 at 10:06
  • 1
    Are the answers here perhaps helpful: https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – Daniel Holmes Nov 22 '18 at 10:22

1 Answers1

1

Add this to your Thing model:

@property
def numbers(self):
    return [1,2,3,4]

Then in your template you could do something like:

{% for number in thing.numbers %}
<li>{{ number }}
{% endfor %}
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • thanks I've learned something but i would like to know how to iterate from 1 to 4 without passing something to the django template. – Bussiere Nov 22 '18 at 10:04
  • You should take a look at this: https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – Red Cricket Nov 22 '18 at 17:44