Daniels0n
Daniels0n

Reputation: 93

Escaping curly braces in flask does not work properly

Here is my code

    <div class="form-group">
        {{ form.target_price.label }}
        {% if price %}
            {{ form.target_price(class="form-control", placeholder='{{ price }}') }}
        {% else %}
            {{ form.target_price(class="form-control") }}
        {% endif %}
    </div>`

When there is a price it should pass in the price as placeholder of the form. But what it does is it pass '{{ price }}' as a string. Any idea why ?

Upvotes: 1

Views: 335

Answers (1)

Menno H&#246;lscher
Menno H&#246;lscher

Reputation: 585

Between double braces you are outside the scope of Jinja2 processing, and into the scope of python processing. In Python the '{{ price }}' text is a string, which will be printed.

To get what you want (the string for the price) use str(price), no quotes or braces; WTForms is smart enough to render in the double quotes required by HTML.

Upvotes: 2

Related Questions