简体   繁体   中英

Comments not working in jinja2

I have a template(test.html) as follows:

{% extends 'base.html' %}
{% from "_formhelpers.html" import render_field %}

{% block content %}

<div class="container">
    <div class="row">
        <div class="span6 offset3">
            <form class="form-horizontal" action="/create_user/" method="post">
                {{ form.csrf_token }}
                <dl>
                    {{ render_field(form.name) }}
                    {{ render_field(form.members) }}
                    <!--<div class="control-group">
                        <label class="control-label">
                            {{ form.task.label }}
                        </label>
                        <div class='controls'>
                            {{ form.task}}

                            {% if form.task.errors %}
                            <ul class="text-error">
                                {% for error in form.task.errors %}
                                    <li>{{ error }}</li>
                                {% endfor %}
                            </ul>
                            {% endif %}
                        </div>
                    </div>-->
                </dl>

            </form>
        </div>
    </div>
</div>

{% endblock %}

When rendering this template using Flask's render_template("test.html", form=form). I got following error "UndefinedError: 'tickapp.forms.TeamForm object' has no attribute 'task'". As you can see I have commented out 'form.task' in the template(whole ) and also there is no such field in models and in my form.

I wonder why jinja2 is considering commented html content. I trusted comments(!) and spent couple of hours on this issue. Finally, deleted all the comments and it started working.Anybody working in jinja2 faced this problem? and do you know why it is happening?

Basically, jinja2 is only concerned with finding an evaluating its own blocks, not the structure of the HTML. If you want to exclude a section of your template entirely, you can use jinja2's comment syntax :

{# This is a comment now.
    <div class="control-group">
       ...
    </div>
#}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM