简体   繁体   English

评论不适用于jinja2

[英]Comments not working in jinja2

I have a template(test.html) as follows: 我有一个模板(test.html),如下所示:

{% 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). 使用Flask的render_template(“ test.html”,form = form)渲染此模板时。 I got following error "UndefinedError: 'tickapp.forms.TeamForm object' has no attribute 'task'". 我收到以下错误“ UndefinedError:'tickapp.forms.TeamForm对象'没有属性'任务'”。 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. 如您所见,我已经在模板(整个)中注释掉了“ form.task”,并且在模型和表单中也没有这样的字段。

I wonder why jinja2 is considering commented html content. 我不知道为什么jinja2正在考虑带注释的html内容。 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? 最后,删除所有注释,它开始起作用。在jinja2中工作的任何人都遇到这个问题吗? 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. 基本上,jinja2仅关注查找评估自己的块,而不关注HTML的结构。 If you want to exclude a section of your template entirely, you can use jinja2's comment syntax : 如果要完全排除模板的一部分,则可以使用jinja2的注释语法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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