Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments not working in jinja2

Tags:

python

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?

like image 311
rajpy Avatar asked Apr 29 '13 10:04

rajpy


1 Answers

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>
#}
like image 167
robots.jpg Avatar answered Sep 20 '22 02:09

robots.jpg