Comments not working in jinja2

核能气质少年 提交于 2019-12-17 04:08:34

问题


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?


回答1:


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>
#}


来源:https://stackoverflow.com/questions/16275877/comments-not-working-in-jinja2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!