2

I have a loop to show a list content in a flask template but I don't want to show the first character of the element , in this way works in python but not in flask

{%for file in files%}
        {% f= file['path'] %}
        <p> {{ f[1:] }}</p>
{% endfor %}

I get this error

TemplateSyntaxError: Encountered unknown tag 'f'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

2 Answers 2

6

You need to set variables if you wish to use them in that way. (Documentation).

That said— you should be able to just do {{ file['path'][1:] }} within your for loop.

Sign up to request clarification or add additional context in comments.

Comments

3

Duplicate of this question.

You have to use the {% set %} template tag to assign variables within a jinja2 template:

{% for file in files %}
    {% set f = file['path'] %}
    <p>{{ f[1:] }}</p>
{% endfor %}

1 Comment

That being said, Doobeh has the right idea: just taking a slice of the dict value avoids this problem altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.