0

My for loop is not working and I'm not sure why.

This is the loop:

{% for i in range({{text|length}} + {{images|length}}) %}

text and images are querysets I have passed to the template from the view.

The error I am getting is:

django.template.exceptions.TemplateSyntaxError: 'for' statements should use the format 'for x in y': for i in range({{text|length}} + {{images|length}})

This doesn't make sense to me, as it looks to me as if this does follow the format suggested by the error.

techraf
  • 64,883
  • 27
  • 193
  • 198
cbuch1800
  • 923
  • 3
  • 11
  • 26

1 Answers1

-1
{% for i in range((text|length) + (images|length)) %}

That should fix it. The {{}} are used for another purpose. I have my own problem in jinja2 and haven't used it before. My loop is:

{% for i in range(string1|length) %}

and it works

Patrick
  • 9
  • 2
  • I have tried this and it has made no difference. I get the exact same error. `django.template.exceptions.TemplateSyntaxError: 'for' statements should use the format 'for x in y': for i in range(text|length + images|length)` – cbuch1800 Feb 03 '18 at 08:19
  • You also have {% endfor %}? and also maybe try ((text|length) + (images|length)). Also do you know if images|length and text|length is defined? try just display {{text|length}} and {{images|length}} somewhere and see what it displays – Patrick Feb 03 '18 at 08:44
  • I have `{% endfor%}`. I have tried displaying `{{text|length}}` and `{{images|length}}` and they both works fine. I have tried `range((text|length) + (images|length))` and get the same error as before. – cbuch1800 Feb 03 '18 at 09:29
  • Check this [answer](https://stackoverflow.com/questions/53180287/jinja-django-for-loop-range-not-working) and how it was solved. It might help you. It seems not to be supported by django. – Cheche Nov 07 '18 at 13:30