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