Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Character Builder Drawing the Bear

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,699 Points

Embedding python 'for' loop in html

I was trying to simplify the code by using a single for loop over all the possibilities. This was my best attempt.

            {% for k, v in DEFAULTS if saves.get(k)%}
                <div class="{{ k }}"><img src="/static/img/bear_items_{{ k }}-{{ saves[k] }}"></div>
            {% endfor %}

there's issues with the k value needing to be in quotations sometimes, and not in quotations other times, which I haven't found a workaround for while working in HTML. I am not sure it would work anyway. Is it not possible to work with variables in a way that isn't super-simplistic while under the constraints of HTML?

Chris Hall
Chris Hall
382 Points

Hey! I thought the same when approaching this task. I managed to get the following code to work:

            {% for accessory in ['shirts', 'pants', 'footwear', 'glasses', 'hat'] %}
                {% if saves.get(accessory) %}
                    <div class="{{ accessory }}"><img src="/static/img/bear_items_{{ accessory }}-{{ saves[accessory] }}.svg"></div>
                {% endif %}
            {% endfor %}