Range not working in for loop

时间:2018-02-03 07:45:51

标签: html django-templates jinja2

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.

1 个答案:

答案 0 :(得分:-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