评论在jinja2中不起作用 [英] Comments not working in jinja2

查看:59
本文介绍了评论在jinja2中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板(test.html),如下所示:

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

使用Flask的render_template("test.html",form = form)渲染此模板时.我收到以下错误"UndefinedError:'tickapp.forms.TeamForm对象'没有属性'任务'". 如您所见,我已经在模板(整个)中注释掉了"form.task",并且在模型和表单中也没有这样的字段.

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.

我想知道jinja2为什么正在考虑带注释的html内容.我信任评论(!),并在此问题上花费了几个小时.最后,删除所有注释,它开始起作用.在jinja2中工作的任何人都遇到这个问题吗?你知道为什么会这样吗?

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?

推荐答案

基本上,jinja2只关注查找评估自己的块,而不关注HTML的结构.如果要完全排除模板的一部分,则可以使用jinja2的注释语法:

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

这篇关于评论在jinja2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆