Comments do not work 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 render_template ("test.html", form = form). I got the following error: "UndefinedError:" tickapp.forms.TeamForm object "does not have a task attribute." As you can see, I commented on "form.task" in the template (in general), and there is also no such field in models and in my form.

I wonder why jinja2 is considering a html content comment. I trusted the comments (!) And spent a couple of hours on this. Finally, deleted all comments and it started working. Anyone working in jinja2 have encountered this problem? and do you know why this is happening?

+6
source share
1 answer

Basically, jinja2 is only looking for an estimate of its own blocks, not an HTML structure. If you want to completely exclude a section of your template, you can use jinja2 comment syntax:

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

Source: https://habr.com/ru/post/943869/


All Articles