Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Drawing the Bear

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,720 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 %}