pythonflaskflask-wtforms

Python Flask if block: jinja2.exceptions.TemplateSyntaxError: unexpected '%'



                <div class="mb-3">
                    {{ form.ism.label(class="form-label") }}
                    
                    {{% if form.ism.errors %}}
                        {{ form.ism(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.ism.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.ism(class="form-control form-control-lg") }}    
                    {{% endif %}}                      
                </div>      

jinja2.exceptions.TemplateSyntaxError:

 unexpected '%'

How can I solve this error?

I looked code again and again, but I could not find the what is real problem.

Debugger saying error at 4. line.

This code:

{{% if form.ism.errors %}}

Solution

  • The syntax for variables is {{ myVar }}

    The syntax for expressions such as if and for is {% if ... %}

    Thus, instead of writing {{% %}}, you should go with {% %} which is the appropriate syntax.