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>
#}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With