Range not working in for loop

坚强是说给别人听的谎言 提交于 2019-12-14 03:06:49

问题


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:


{% 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



来源:https://stackoverflow.com/questions/48595150/range-not-working-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!