1
class fruit(flaskform):
fruit1 = "apple"
fruit2 = "orange"
fruit3 = "grapes"

I am trying to access the variables in a for loop in my html page.

{% for index in range(3) %}
    {{"fruit" + index}}
{%endfor%}

Is this possible? I would like to avoid writing 100 lines of code to access the variables if I have 100 fruits. I am new to flask and html. My question could be unclear and feel free to ask me if more info is needed.

3
  • Try a list of fruit names instead. Commented Aug 7, 2018 at 18:49
  • class fruit(flaskform): fruit = ["apple", "orange", "graps"] in HTML: {% for index in range(3) %} {{"fruit" + index}} {%endfor%} Like this? Commented Aug 7, 2018 at 19:01
  • @calvert Take a look at my answer. Commented Aug 7, 2018 at 20:36

1 Answer 1

2

You can store your fruits in a list like this:

fruits = ['apple', 'orange', 'grapes']

Then, you can use a for loop in Jinja2 to iterate through your list and display each individual item.

{% for fruit in fruits %}
    {{ fruit }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

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.