0

Getting an error when try to loop in django template, {% for i in obj.rating %}, where obj.rating is integer value.

i tried to add range() {% for i in range(obj.rating) %}, but its not working.

{% for i in obj.rating %}
  <span class="fa fa-star blue-star" id="star1"></span>
{% endfor %}
Saran Prasad
  • 125
  • 2
  • 13
  • Various solutions are discussed here: https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates – Mark Bailey May 01 '19 at 20:17

2 Answers2

1

Try this

{% for i in i|rjust:obj.rating %}
  <span class="fa fa-star blue-star" id="star1">some text</span>
{% endfor %}
Mehran Nouri
  • 136
  • 15
  • I tried, but in that loop statement obj.rating is returning None i think(hence for i in i, runes for 4 times), but when in put {{ obj.rating }} inside loop it shows the value. but not in the for loop statement why so? – Saran Prasad May 01 '19 at 11:23
  • I don't know why, did you write the code correctly without any syntax error? – Mehran Nouri May 01 '19 at 13:19
  • thanks, this worked for me. please edit i to "i". {% for i in "x"|ljust:obj.rating %} {% endfor %} – Saran Prasad May 07 '19 at 09:34
0

In your python code make a list like below and then pass it to template to iterate over it.

list = []
for item in range(rating):
    list.append(item)

make sure to end the loop by

{% endfor %}
chirag soni
  • 936
  • 10
  • 20